【发布时间】:2014-07-22 23:41:48
【问题描述】:
我得到的错误是“StrictMode$AndroidBlockGuardPolicy.onNetwork”使用
我在这里尝试做的是使用 jsonobject 将所有这些数据发布到服务器。我真的不知道在这里做什么,因为我正在使用一个活动和这个类来管理所有的 http 请求。
这是我的代码:
import android.util.Log;
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.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import java.io.IOException;
public class HttpRequestManager {
String TAG = "Request Manager";
public String RegisterUser (String deviceId, String userNames, String userSurnames, String password,
String retypedPassword, String userIds,
String userCellphone, String userEmail) {
//Log.i(TAG, deviceId);
JSONObject registerFormObject = new JSONObject();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://192.168.1.110:8000/api/v1/registration");
try {
try {
registerFormObject.put("device_id", deviceId);
registerFormObject.put("device_type", "a");
registerFormObject.put("names", userNames);
registerFormObject.put("surnames", userSurnames);
registerFormObject.put("cell", userCellphone);
registerFormObject.put("email", userEmail);
registerFormObject.put("password1", password);
registerFormObject.put("password2", retypedPassword);
registerFormObject.put("identification", userIds);
}catch (Exception e){
}
httpPost.setEntity(new StringEntity(registerFormObject.toString()));
//httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(httpPost);
Log.i(TAG, response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.i(TAG, e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i(TAG, e.toString());
}
return userSurnames;
}
}
知道如何解决这个错误吗?
uses-permission android:name="android.permission.INTERNET" 顺便说一句。
【问题讨论】:
-
Android 现在禁止在主线程上进行网络操作。试试 AsyncTask - 见stackoverflow.com/questions/24898370/…
标签: java android httpclient jsonobject