我在提交数据时遇到问题,android.os.NetworkOnMainThreadException出现问题。请帮我解决这个问题。 以下来源daftar_nama_lokasi.java:
package geotracker.lokasi;
public class daftar_nama_lokasi extends Activity{
private LocationManager lm;
private LocationListener locationListener;
private EditText lat;
private EditText lon;
private EditText nama_lokasi;
private EditText alamat_lokasi;
private EditText kode;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.daftar_nama_lokasi);
lat = (EditText) findViewById(R.id.latitude);
lon = (EditText) findViewById(R.id.longitude);
nama_lokasi = (EditText) findViewById(R.id.nama_lokasi);
alamat_lokasi = (EditText) findViewById(R.id.alamat_lokasi);
kode = (EditText) findViewById(R.id.imei);
lat.setEnabled(false);
lon.setEnabled(false);
kode.setEnabled(false);
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
// mendapatkan nomor imei dan menyimpanya dalam variabel
String imei = telephonyManager.getDeviceId();
EditText IMEI = (EditText)findViewById(R.id.imei);
// menampilkan nomor imei pada EditView yang sudah di buat
IMEI.setText(imei);
// ---use the LocationManager class to obtain GPS locations---
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Button btnSimpan = (Button) findViewById(R.id.btnsimpan);
btnSimpan.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
JSONObject json = new JSONObject();
String hasil = "";
try {
json.put("lat", lat.getText());
json.put("lon", lon.getText());
json.put("location_name", nama_lokasi.getText());
json.put("address", alamat_lokasi.getText());
json.put("imei", kode.getText());
koneksi_internet kn = new koneksi_internet();
hasil = kn.postData(json,"nama_lokasi");
Toast.makeText(getBaseContext(), hasil, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
}
private class MyLocationListener implements LocationListener {
//Menampilkan Update Lokasi GPS terkini
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
lat.setText(String.valueOf(loc.getLatitude()));
lon.setText(String.valueOf(loc.getLongitude()));
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}//end class
konfigurasi.java
package geotracker.lokasi;
public class konfigurasi {
final static String URL ="http://localhost/andro/config/index.php?jenis=";
final static int WAKTU = 1;// 1 menit
}
koneksi_internet.java
package geotracker.lokasi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class koneksi_internet {
public String test(){
return konfigurasi.URL;
}
public String postData(JSONObject json, String querystring) throws JSONException{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(konfigurasi.URL+querystring);
//JSONObject json = new JSONObject();
String text="";
try {
// JSON data:
JSONArray postjson=new JSONArray();
postjson.put(json);
// Post the data:
httppost.setHeader("json",json.toString());
httppost.getParams().setParameter("jsonpost",postjson);
// Execute HTTP Post Request
System.out.print(json);
HttpResponse response = httpclient.execute(httppost);
// for JSON:
if(response != null)
{
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");// backslash \
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
text = sb.toString();
}
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return text;
}
}
答案 0 :(得分:1)
Android需要在单独的线程中完成数据库查询或网络操作等长时间操作。为此使用AsyncTask,因为它提供了无痛的线程,因为它可以同步你的Ui和后台线程。那么在AsyncTask的doInBackground函数中进行网络操作。
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
postData();
return null;
}
@Override
protected void onPostExecute(String result) {
// runs in ui thread so update views here
}
@Override
protected void onPreExecute() {
//runs in ui thread start progress dialog here
}
}