好了,所以我在一个新的类创建的方法,并呼吁它从我的活动try catch块,当我把它与通过我的字符串值,我的问题出现了...Android的HTTP POST错误
我的问题之后执行下述方法之后开始:
HttpResponse httpresponse = httpclient.execute(httppostreq);
它去捕捉(IOException异常e)在我的活动,当我试图打印响应字符串它给了我来自服务器的响应!!!!
所以,问题是,当我试图值传递给POST它应该返回一些数据,但是失败了,它是从服务器返回空消息
提示: 将出现空消息,如果没有值
jsonobj.put("screenType", requestString);
所以我通过了价值或不?以及为什么它会导致异常?
public void postData(String requestString) throws JSONException, ClientProtocolException, IOException {
// Create a new HttpClient and Post Header
DefaultHttpClient httpclient = new DefaultHttpClient();
JSONObject jsonobj = new JSONObject();
jsonobj.put("screenType", requestString);
//jsonobj.put("old_passw", "306");
HttpPost httppostreq = new HttpPost("mysite.org");
StringEntity se = new StringEntity(jsonobj.toString());
se.setContentType("application/json;charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
httppostreq.setEntity(se);
HttpResponse httpresponse = httpclient.execute(httppostreq);
Log.i("in try", httpresponse.toString());
String responseText=null;
try {
responseText=EntityUtils.toString(httpresponse.getEntity());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Log.i("in Exception",e.toString());
Log.i("arse exception", httpresponse.toString());
}
}
我还添加了互联网permisssion
<uses-permission android:name="android.permission.INTERNET" />
我在第一次活动中没有做第二次活动 – 2015-03-26 06:06:30
如果您在主线程上进行调用,会发现错误。您必须在背景上拨打电话(例如AsynTaskLoader) – vincent091 2015-03-26 08:13:43
在我的实例活动中,我有一个按钮,当用户单击它时,它将打开摄像头以读取条形码,并从此处打开另一个活动,并在打开创建方法我发送请求是错误的发送它在布局的活动? – 2015-03-27 08:18:16