从URL获取并将JSON数组/ JSON对象显示到Android应用程序的textView中

时间:2015-06-02 07:53:05

标签: android json

package com.tricks.readjsonfromurl;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads()
                .detectDiskWrites().detectNetwork().penaltyLog().build());

        TextView showJSON = (TextView) findViewById(R.id.JSONfromURL);

        JSONObject json = null;

        String str = "";

        HttpResponse response;

        HttpClient myClient = new DefaultHttpClient();

//这是我想从中获取JSON数组并将其显示到textView中的URL。

        HttpPost myConnection = new HttpPost("http://128.199.77.178:8080/discvrweb/getorders/1/20/createdDate");
        try
    {
            response = myClient.execute(myConnection);
                str = EntityUtils.toString(response.getEntity(), "UTF-8");      
        }

    catch (ClientProtocolException e)
    {
            e.printStackTrace();
        }

    catch (IOException e)
    {
            e.printStackTrace();
        }

        try
    {
            JSONArray jArray = new JSONArray(str);
            json = jArray.getJSONObject(0);

        //Here is a String variable in which i want to store the JSON Array/JSON Object.

        String showJSONIntoTextView = jArray.toString();  

        //Now all i just want to do is to show the JSON Array/JSON Object
        // fetched from the above URL to be shown in showJSON textView

        showJSON.setText(showJSONIntoTextView);

        // But showJSON textview shows up empty without any JSON String. 

        }

    catch ( JSONException e){e.printStackTrace();}

    }
}

1 个答案:

答案 0 :(得分:0)

试试这段代码,

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;


public class ServerTest extends Activity {

    private String TAG = "test";
    private String url = "http://128.199.77.178:8080/discvrweb/getorders/1/20/createdDate";

    TextView showJSON;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showJSON = (TextView) findViewById(R.id.JSONfromURL);

        new Download().execute();
    }


    public class Download extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {
            String out = null;

            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();

                final HttpParams httpParameters = httpClient.getParams();

                HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
                HttpConnectionParams.setSoTimeout(httpParameters, 15000);

                HttpGet httpPost = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();

                out = EntityUtils.toString(httpEntity, HTTP.UTF_8);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return out;
        }


        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            showJSON.setText(result);
            Log.e(TAG, result);
        }
    }
}

还要确保已将此添加到清单

<uses-permission android:name="android.permission.INTERNET" />