【发布时间】:2015-03-25 08:10:59
【问题描述】:
好的,所以我在一个新类中创建了一个方法,并从我在 try catch 块中的活动中调用它,当我调用它并传递我的字符串值时,我的问题出现了......
我的问题是在执行以下方法后开始的:
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());
}
}
我还添加了互联网权限
<uses-permission android:name="android.permission.INTERNET" />
【问题讨论】:
标签: android post httprequest