0
的logcat:應用程序崩潰與JSON
10-25 01:54:33.670: E/Trace(1693): error opening trace file: No such file or directory (2)
10-25 01:54:51.139: E/AndroidRuntime(1693): FATAL EXCEPTION: main
10-25 01:54:51.139: E/AndroidRuntime(1693): android.os.NetworkOnMainThreadException
10-25 01:54:51.139: E/AndroidRuntime(1693): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
10-25 01:54:51.139: E/AndroidRuntime(1693): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-25 01:54:51.139: E/AndroidRuntime(1693): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-25 01:54:51.139: E/AndroidRuntime(1693): at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-25 01:54:51.139: E/AndroidRuntime(1693): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-25 01:54:51.139: E/AndroidRuntime(1693): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-25 01:54:51.139: E/AndroidRuntime(1693): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-25 01:54:51.139: E/AndroidRuntime(1693): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-25 01:54:51.139: E/AndroidRuntime(1693): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-25 01:54:51.139: E/AndroidRuntime(1693): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-25 01:54:51.139: E/AndroidRuntime(1693): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-25 01:54:51.139: E/AndroidRuntime(1693): at app.nightfind.easynight.JSONParser.makeHttpRequest(JSONParser.java:40)
10-25 01:54:51.139: E/AndroidRuntime(1693): at app.nightfind.easynight.ViewBusiness$GetProductDetails$1.run(ViewBusiness.java:97)
10-25 01:54:51.139: E/AndroidRuntime(1693): at android.os.Handler.handleCallback(Handler.java:615)
10-25 01:54:51.139: E/AndroidRuntime(1693): at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 01:54:51.139: E/AndroidRuntime(1693): at android.os.Looper.loop(Looper.java:137)
10-25 01:54:51.139: E/AndroidRuntime(1693): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-25 01:54:51.139: E/AndroidRuntime(1693): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 01:54:51.139: E/AndroidRuntime(1693): at java.lang.reflect.Method.invoke(Method.java:511)
10-25 01:54:51.139: E/AndroidRuntime(1693): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-25 01:54:51.139: E/AndroidRuntime(1693): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-25 01:54:51.139: E/AndroidRuntime(1693): at dalvik.system.NativeStart.main(Native Method)
10-25 01:54:54.090: E/InputDispatcher(158): channel '41232b18 app.nightfind.easynight/app.nightfind.easynight.EasyNight (server)' ~ Channel is unrecoverably broken and will be disposed!
10-25 01:54:54.160: E/InputDispatcher(158): Received spurious receive callback for unknown input channel. fd=156, events=0x9
10-25 01:54:54.160: E/InputDispatcher(158): Received spurious receive callback for unknown input channel. fd=160, events=0x9
10-25 01:54:54.160: E/InputDispatcher(158): Received spurious receive callback for unknown input channel. fd=161, events=0x9
這是JSONparser這就是行做出錯誤:
HttpResponse httpResponse = httpClient.execute(httpGet);
這是JSONPARSER:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONObject makeHttpRequest(String url,
List<NameValuePair> params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
這是ViewBusiness中的行:
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, params);
,這是VieBusiness: 的「PID」我可是從另一個活動讓我試了一下沒有「PID」,但同樣的錯誤滾滾而來..
public class ViewBusiness extends Activity {
TextView tvName, tvDesc, tvAddress, tvCity;
String pid;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String url_product_detials = "http://******";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCT = "product";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_DESC = "desc";
private static final String TAG_CITY = "city";
private static final String TAG_ADDRESS = "address";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.business);
Intent i = getIntent();
pid = i.getStringExtra(TAG_PID);
new GetProductDetails().execute();
}
class GetProductDetails extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ViewBusiness.this);
pDialog.setMessage("LOADING");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... params) {
runOnUiThread(new Runnable() {
public void run() {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, params);
if(json == null){
cancel(true);
pDialog.dismiss();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
ViewBusiness.this);
alertDialogBuilder.setTitle("ERROR");
alertDialogBuilder
.setMessage(R.string.connectionp)
.setCancelable(false)
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
ViewBusiness.this.finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}else{
Log.d("Single Product Details", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT);
JSONObject product = productObj.getJSONObject(0);
tvName = (EditText) findViewById(R.id.name);
tvDesc = (EditText) findViewById(R.id.desc);
tvAddress = (EditText) findViewById(R.id.address);
tvCity = (EditText) findViewById(R.id.city);
tvName.setText(product.getString(TAG_NAME));
tvDesc.setText(product.getString(TAG_DESC));
tvAddress.setText(product.getString(TAG_ADDRESS));
tvCity.setText(product.getString(TAG_CITY));
}else{
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
}
我找不到解決方案..我如何將所有視圖移動到「onPostExecute」? – iZikoo