ANDROID,从Web服务器解析JSON数据并显示在ListView上

用户名

我正在尝试显示JSON URL链接的JSON结果。目前,当我加载时,它什么也不显示,只是空白页。这是我获得有关JSON信息的来源

这是我的代码:

public class DVLAresult extends AppCompatActivity {


    public class DVLAlist extends ListActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.content_dvlaresult);


            setListAdapter(new ArrayAdapter(
                    this,android.R.layout.simple_list_item_2,
                    this.populate()));
        }


        private ArrayList<String> populate() {
            ArrayList<String> items = new ArrayList<String>();

            TextView newtext = (TextView) findViewById(R.id.view_number);

            try {
                URL url = new URL
                        ("https://dvlasearch.appspot.com/DvlaSearch?licencePlate=mt09nks&apikey=DvlaSearchDemoAccount");
                HttpURLConnection urlConnection =
                        (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();
                // gets the server json data
                BufferedReader bufferedReader =
                        new BufferedReader(new InputStreamReader(
                                urlConnection.getInputStream()));
                String next;
                while ((next = bufferedReader.readLine()) != null) {
                    JSONArray ja = new JSONArray(next);

                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = (JSONObject) ja.get(i);
                        items.add(jo.getString("text"));
                    }
                }
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return items;
        }
    }
}

这是我的XML文件simple_list_2.xml

<?xml version="1.0" encoding="utf-8"?>

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/listPreferredItemHeight"
    android:mode="twoLine"
    android:paddingStart="?attr/listPreferredItemPaddingStart"
    android:paddingEnd="?attr/listPreferredItemPaddingEnd">

    <TextView android:id="@id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textAppearance="?attr/textAppearanceListItem" />

    <TextView android:id="@id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:layout_alignStart="@id/text1"
        android:textAppearance="?attr/textAppearanceListItemSecondary" />

    <TextView android:id="@id/text3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text2"
        android:layout_alignStart="@id/text2"
        android:textAppearance="?attr/textAppearanceListItemSecondary" />

... Continue up to text18, because I have 18 fields.

</TwoLineListItem>

这是主要的XML ListView

 <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/button2"
        android:layout_below="@+id/view_number" />
马亨德拉·古纳瓦德纳(Mahendra Gunawardena)

我碰巧遇到类似的问题。这是帮助我找到分辨率的代码。希望这会帮助你。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SEND GET REQUEST"
        android:id="@+id/sendGet"
        android:onClick="sendGetRequest"
        android:layout_alignParentStart="true" />

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        android:layout_below="@+id/sendGet"
        android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Response ....."
            android:id="@+id/showOutput"
            android:layout_alignEnd="@+id/scrollView"
            android:layout_marginEnd="34dp" />

    </ScrollView>

</RelativeLayout>

MainActivity.java

import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private ProgressDialog progress;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
 //   private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
  //      client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    public void sendGetRequest(View View) {
        new GetClass(this).execute();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
 //       client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.gunawardena.get_post_demo/http/host/path")
        );
 //       AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.gunawardena.get_post_demo/http/host/path")
        );
      //  AppIndex.AppIndexApi.end(client, viewAction);
      // client.disconnect();
    }

    private class GetClass extends AsyncTask<String, Void, Void> {

        private final Context context;

        public GetClass(Context c) {
            this.context = c;
        }

        protected void onPreExecute() {
            progress = new ProgressDialog(this.context);
            progress.setMessage("Loading Get Method.....");
            progress.show();
        }

        @Override
        protected Void doInBackground(String... params) {
            try {

                final TextView outputView = (TextView) findViewById(R.id.showOutput);
                URL url = new URL("https://dvlasearch.appspot.com/DvlaSearch?licencePlate=mt09nks&apikey=DvlaSearchDemoAccount");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                connection.setRequestMethod("GET");
                connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
                connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");

                int responseCode = connection.getResponseCode();

                final StringBuilder output = new StringBuilder("Request URL " + url);
                output.append(System.getProperty("line.separator") + "Response Code " + responseCode);
                output.append(System.getProperty("line.separator") + "Type " + "GET");
                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line = "";
                StringBuilder responseOutput = new StringBuilder();
                System.out.println("output===============" + br);
                while ((line = br.readLine()) != null) {
                    responseOutput.append(line);
                }
                br.close();

                output.append(System.getProperty("line.separator") + "Response " + System.getProperty("line.separator") + System.getProperty("line.separator") + responseOutput.toString());

                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        outputView.setText(output);
                        progress.dismiss();

                    }
                });

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    }
}

在仿真器上输出

在此处输入图片说明

高温超导


参考:

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ANDROID,从Web服务器解析JSON数据并显示在ListView上

来自分类Dev

如何在服务器上显示实时JSON数据

来自分类Dev

如何在Android中使用RecyclerView从Web服务器显示JSON格式数据

来自分类Dev

在android上显示JSON解析器listview

来自分类Dev

Android检查Web服务器上的新数据

来自分类Dev

从远程服务器解析JSON数据

来自分类Dev

Web服务器上的本地数据

来自分类Dev

使用JSON xmlHttprequest从Web服务器读取数据库并显示数据?

来自分类Dev

Android JSON解析器将数据的多个副本发送到服务器

来自分类Dev

如何通过Web服务器在您的应用程序上显示最新数据

来自分类Dev

Web 服务的解析服务器和数据库服务器之间的区别

来自分类Dev

从服务器获取数据时在Android中使用Listview

来自分类Dev

Android ListView从服务器加载更多数据

来自分类Dev

无法解析服务器中的JSON数据

来自分类Dev

无法解析服务器中的JSON数据

来自分类Dev

使用地图数据从服务器解析 JSON

来自分类Dev

如何使用授权在服务器上上传图像并在 Android 中显示收到的 json 响应?

来自分类Dev

JSON文件未显示在Godaddy服务器上

来自分类Dev

FutureBuilder未显示从服务器获取的json数据

来自分类Dev

如何使用Android在localhost上创建(Web)服务器

来自分类Dev

在Go WebSocket服务器上获取JSON数据

来自分类Dev

Web服务器/数据库在线与android通信?

来自分类Dev

通过Android在服务器上保存连续位置数据

来自分类Dev

通过Android在服务器上保存连续位置数据

来自分类Dev

Firebase从服务器更改数据,在Android上侦听

来自分类Dev

在Android中将数据上传到Internet上的服务器

来自分类Dev

在解析服务器上配置APNS

来自分类Dev

无法从android应用中的服务器获取json数据

来自分类Dev

如何在Azure日志分析上显示Azure App服务的Web服务器日志?

Related 相关文章

  1. 1

    ANDROID,从Web服务器解析JSON数据并显示在ListView上

  2. 2

    如何在服务器上显示实时JSON数据

  3. 3

    如何在Android中使用RecyclerView从Web服务器显示JSON格式数据

  4. 4

    在android上显示JSON解析器listview

  5. 5

    Android检查Web服务器上的新数据

  6. 6

    从远程服务器解析JSON数据

  7. 7

    Web服务器上的本地数据

  8. 8

    使用JSON xmlHttprequest从Web服务器读取数据库并显示数据?

  9. 9

    Android JSON解析器将数据的多个副本发送到服务器

  10. 10

    如何通过Web服务器在您的应用程序上显示最新数据

  11. 11

    Web 服务的解析服务器和数据库服务器之间的区别

  12. 12

    从服务器获取数据时在Android中使用Listview

  13. 13

    Android ListView从服务器加载更多数据

  14. 14

    无法解析服务器中的JSON数据

  15. 15

    无法解析服务器中的JSON数据

  16. 16

    使用地图数据从服务器解析 JSON

  17. 17

    如何使用授权在服务器上上传图像并在 Android 中显示收到的 json 响应?

  18. 18

    JSON文件未显示在Godaddy服务器上

  19. 19

    FutureBuilder未显示从服务器获取的json数据

  20. 20

    如何使用Android在localhost上创建(Web)服务器

  21. 21

    在Go WebSocket服务器上获取JSON数据

  22. 22

    Web服务器/数据库在线与android通信?

  23. 23

    通过Android在服务器上保存连续位置数据

  24. 24

    通过Android在服务器上保存连续位置数据

  25. 25

    Firebase从服务器更改数据,在Android上侦听

  26. 26

    在Android中将数据上传到Internet上的服务器

  27. 27

    在解析服务器上配置APNS

  28. 28

    无法从android应用中的服务器获取json数据

  29. 29

    如何在Azure日志分析上显示Azure App服务的Web服务器日志?

热门标签

归档