获得致命异常。我希望通过php api从服务器响应。但每次我收到此错误
10-01 07:16:35.627: E/AndroidRuntime(1314): FATAL EXCEPTION: main
10-01 07:16:35.627: E/AndroidRuntime(1314): Process: com.example.googlemap, PID: 1314
10-01 07:16:35.627: E/AndroidRuntime(1314): android.os.NetworkOnMainThreadException
10-01 07:16:35.627: E/AndroidRuntime(1314): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
10-01 07:16:35.627: E/AndroidRuntime(1314): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-01 07:16:35.627: E/AndroidRuntime(1314): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-01 07:16:35.627: E/AndroidRuntime(1314): at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-01 07:16:35.627: E/AndroidRuntime(1314): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-01 07:16:35.627: E/AndroidRuntime(1314): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-01 07:16:35.627: E/AndroidRuntime(1314): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-01 07:16:35.627: E/AndroidRuntime(1314): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-01 07:16:35.627: E/AndroidRuntime(1314): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-01 07:16:35.627: E/AndroidRuntime(1314): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-01 07:16:35.627: E/AndroidRuntime(1314): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-01 07:16:35.627: E/AndroidRuntime(1314): at com.vss.lcm.gps.JSONPARSER1.makeHttpRequest(JSONPARSER1.java:64)
10-01 07:16:35.627: E/AndroidRuntime(1314): at com.vss.lcm.gps.Supervisornew$GetContacts$1.run(Supervisornew.java:97)
10-01 07:16:35.627: E/AndroidRuntime(1314): at android.os.Handler.handleCallback(Handler.java:733)
10-01 07:16:35.627: E/AndroidRuntime(1314): at android.os.Handler.dispatchMessage(Handler.java:95)
10-01 07:16:35.627: E/AndroidRuntime(1314): at android.os.Looper.loop(Looper.java:136)
10-01 07:16:35.627: E/AndroidRuntime(1314): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-01 07:16:35.627: E/AndroidRuntime(1314): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 07:16:35.627: E/AndroidRuntime(1314): at java.lang.reflect.Method.invoke(Method.java:515)
10-01 07:16:35.627: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-01 07:16:35.627: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-01 07:16:35.627: E/AndroidRuntime(1314): at dalvik.system.NativeStart.main(Native Method)
我的代码是
package com.vss.lcm.gps;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.googlemap.MainActivity2;
import com.example.googlemap.R;
public class Supervisornew extends Activity {
Button bt;
private ProgressDialog pDialog;
String id = "9999999999";
private static String url_detail = "http://dyandroidapps.netii.net/gettinglatlngdynamically.php";
// URL to get contacts JSON
// private static String url =
// "http://dyandroidapps.netii.net/android_db/newdymodifiedserver.php";
// JSON parser class
JSONPARSER1 jsonParser = new JSONPARSER1();
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CONTACTS = "product";
private static final String TAG_LAT = "lat";
private static final String TAG_LNG = "lng";
// contacts JSONArray
// JSONArray contacts = null;
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.supervisor);
bt = (Button) findViewById(R.id.btn_supervisor);
contactList = new ArrayList<HashMap<String, String>>();
}
public void track(View v) {
// new LoadAllProducts().execute();
new GetContacts().execute();
// new RetrieveTask().execute();
}
// private class GetContacts extends AsyncTask<Void, ArrayList<String>,
// Void>
class GetContacts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Supervisornew.this);
pDialog.setMessage("Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_detail, "GET", params);
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_CONTACTS); // JSON Array
// get first product object from JSON Array
JSONObject contacts = productObj.getJSONObject(0);
}
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String lat = c.getString(TAG_LAT);
String lng = c.getString(TAG_LNG);
Toast.makeText(getApplicationContext(),
"lat" + lat, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),
"lng" + lng, Toast.LENGTH_SHORT).show();
Log.d("response", lat);
Log.d("response", lng);// here i m getting values in
// log
HashMap<String, String> map = new HashMap<String, String>();
map.put("lat", lat);
map.put("lng", lng);
Toast.makeText(getApplicationContext(), lat,
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), lng,
Toast.LENGTH_SHORT).show();
contactList.add(map);
}
if (contactList != null) {
Intent intentmap = new Intent(Supervisornew.this,
MainActivity2.class);
intentmap.putExtra("arraylist", contactList);
startActivityForResult(intentmap, 500);
}
else {
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
}
我的JsonParser文件是
public class JSONPARSER1 {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONPARSER1() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if (method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
// request method is GET
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, "iso-8859-1"), 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 parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
我无法理解为什么我会收到此错误。请给我一些建议。