在Android应用程序中获取json数组

在Android应用程序中获取json数组,android,json,Android,Json,我无法使用给定的URL从Android应用程序中的json数组中获取某些字段 package com.tricks.readjsonfromurl; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.a

我无法使用给定的URL从Android应用程序中的json数组中获取某些字段

package com.tricks.readjsonfromurl;

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;


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 oid_from_JSON = (TextView) findViewById(R.id.JSON_Order_ID_From_URL);
        TextView order_status_from_JSON = (TextView) findViewById(R.id.JSON_Order_Status_From_URL);
        TextView city_name_from_JSON = (TextView) findViewById(R.id.JSON_City_Name_From_URL);

        JSONObject json = null;

        String str = "";
        HttpResponse response;
        HttpClient myClient = new DefaultHttpClient();

        HttpPost myConnection = new HttpPost("http://128.199.77.178:8080/discvrweb/getorders/1/20/createdDate");


        // In this myConnection object i have passed URL from which i want to get my JSON string.


        try
        {
            response = myClient.execute(myConnection);
            str = EntityUtils.toString(response.getEntity(), "UTF-8");
            //showJSON.setText(str);
        }

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

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


        try{

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

            String showJSONIntoTextView = jArray.toString();

            showJSON.setText(showJSONIntoTextView);

            // Here in showJSON textView i want to show the JSON Array but Nothing shows up when i am running my application.
            // And same in case of all three fields given below.

            oid_from_JSON.setText(json.getString("order_id"));
            order_status_from_JSON.setText(json.getString("order_status"));
            city_name_from_JSON.setText(json.getString("city"));

        }
        catch ( JSONException e)
        {
            e.printStackTrace();                
        }
    }
}
用这个,

public class MainActivity extends ActionBarActivity {

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

public class NewRequest extends AsyncTask<String, Integer, JSONArray> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected JSONArray doInBackground(String... params) {

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpGet httpGet = new HttpGet(
                    "http://128.199.77.178:8080/discvrweb/getorders/1/20/createdDate");

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

            String response = EntityUtils.toString(httpEntity);

            return new JSONArray(response);

        } catch (Exception e) {
            Log.e("Exception", e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(JSONArray result) {
        super.onPostExecute(result);

        try {
            JSONObject jsonObject = result.getJSONObject(0);

            String order_id = jsonObject.getString("order_id");
            String created_at = jsonObject.getString("created_at");
            String updated_at = jsonObject.getString("updated_at");
            String order_status = jsonObject.getString("order_status");

            JSONArray items = jsonObject.getJSONArray("items");

            System.out.println(order_id);
            System.out.println(created_at);
            System.out.println(updated_at);
            System.out.println(order_status);

            for (int i = 0; i < items.length(); i++) {

                JSONObject jsonObject2 = items.getJSONObject(i);

                String pid = jsonObject2.getString("pid");
                String units = jsonObject2.getString("units");
                String unit_selling_price = jsonObject2
                        .getString("unit_selling_price");
                String total_price = jsonObject2.getString("total_price");

                System.out.println(pid);
                System.out.println(units);
                System.out.println(unit_selling_price);
                System.out.println(total_price);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
}
公共类MainActivity扩展了ActionBarActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建NewRequest().execute();
}
公共类NewRequest扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的JSONArray doInBackground(字符串…参数){
试一试{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpGet HttpGet=新HttpGet(
"http://128.199.77.178:8080/discvrweb/getorders/1/20/createdDate");
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
字符串响应=EntityUtils.toString(httpEntity);
返回新的JSONArray(响应);
}捕获(例外e){
Log.e(“异常”,例如toString());
}
返回null;
}
@凌驾
受保护的void onPostExecute(JSONArray结果){
super.onPostExecute(结果);
试一试{
JSONObject JSONObject=result.getJSONObject(0);
String order_id=jsonObject.getString(“order_id”);
String created_at=jsonObject.getString(“created_at”);
String updated_at=jsonObject.getString(“updated_at”);
String order_status=jsonObject.getString(“order_status”);
JSONArray items=jsonObject.getJSONArray(“items”);
系统输出打印项次(订单号);
System.out.println(在创建时创建);
System.out.println(更新时间);
系统输出打印项次(订单状态);
对于(int i=0;i

我希望这将解决您的问题

您应该尝试调试您的异常。。记录catch子句并查找错误。。也许这只是一个打字错误。。我建议下次再问你一个更好的问题。就目前情况而言,这个问题很难理解。请务必阅读