我正在尝试使用JSON对象在android上制作post方法,我能够创建post方法所需的JSON文件,一切似乎都很好,我只是没有得到结果,我结果空虚。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView1);
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
JSONObject jsonObj = new JSONObject();
try {
jsonObj.put("IdSurvey", "102");
jsonObj.put("IdUser", "toto");
jsonObj.put("UserInformation", "toto de Paris");
JSONArray jsonArr = new JSONArray();
JSONObject j= new JSONObject();
j.put("IdQuestion", "5766");
j.put("IdProposition", "9716");
j.put("Time", "32");
jsonArr.put(j);
jsonObj.put("List", jsonArr);
String result = POST("http://tv3.orangeadd.com/answers/102?owner=ofc&user=toto",jsonObj);
text.setText(result);
Toast.makeText(MainActivity.this, "result "+result, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String POST(String url, JSONObject jso){
InputStream inputStream = null;
String result = "empty";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
json = jso.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
}
// 11. return result
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line ="";
String result ="";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
好吧,我没有收到任何错误,只是空洞的结果!谢谢你的家伙