我无法使用给定的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();
}
}
}
答案 0 :(得分:0)
使用此,
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();
}
}
}
}
我希望这能解决你的问题