我想用一些參數向服務器發送一個json請求。請求會和異步任務是工作的罰款,但它在服務器拋出異常,說無效的網址使用異步任務發送帶有參數的json請求android
下面是我在做什麼
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button btnChart = (Button) findViewById(R.id.btn_chart);
// Defining click event listener for the button btn_chart
OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
new HttpAsyncTask().execute("https://tt.student.com/back.json");
}
};
// Setting event click listener for the button btn_chart of the MainActivity layout
btnChart.setOnClickListener(clickListener);
}
public static String POST(String url){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = getNewHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("user", 1);
jsonObject.accumulate("student_id", 1);
jsonObject.accumulate("user_email", "[email protected]");
jsonObject.accumulate("from", "Fri Oct 10 12:38:00 2014 GMT+0200");
jsonObject.accumulate("to", "Sat Oct 11 12:38:00 2014 GMT+0200");
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 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;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return POST(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
Log.d(TAG,result);
}
}
在URL中我都嘗試的方法之一是在上面的代碼和另一種是在url本身傳遞參數https://tt.student.com/back.json?user=1&student_id=1&[email protected]&from=Fri Oct 10 12:38:00 2014 GMT+0200&to=Sat Oct 11 12:38:00 2014 GMT+0200
爲此它說URL中的非法字符...
而不是GET請求你應該使用POST請求,電子郵件ID將被視爲無效的查詢字符串。 – 2014-10-11 11:25:33
GET只是方法,但裏面我正在做POST請求,爲什麼電子郵件將無效? – user3290805 2014-10-11 11:26:57
添加服務器端代碼也 – 2014-10-11 11:31:56