[英]Failed to post/upload data to server from Android
I got stuck on this, I don't understand where the problem comes from. 我对此感到困惑,我不明白问题的来源。 The API was tested successfully with Postman, but fails on my app. 使用Postman成功测试了API,但在我的应用程序上失败了。
My code is: 我的代码是:
public void posting() {
String title = titleArticle.getText().toString();
// String category = spinnerCat.getCount();
String content = contentArticle.getText().toString();
// Instantiate Http Request Param Object
RequestParams params = new RequestParams();
params.put("title", title);
params.put("content", content);
// Invoke RESTful Web Service with Http parameters
invokeWS(params);
}
/**
* Method that performs RESTful webservice invocations
*
* @param params
*/
public void invokeWS(RequestParams params) {
// Show Progress Dialog
prgDialog.show();
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://openetizen.com/api/v1/articles", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// Hide Progress Dialog
prgDialog.hide();
try {
JSONObject obj = new JSONObject(new String(responseBody));
if (obj.getString("status").equalsIgnoreCase("success")) {
Toast.makeText(getApplicationContext(), "Artikel berhasil diunggah!", Toast.LENGTH_LONG).show();
Intent i = new Intent(PostingActivity.this, MainActivity.class);
startActivity(i);
} else {
errorMsg.setText(obj.getString("error_msg"));
Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
Log.e("ERROR", "Response");
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// Hide Progress Dialog
prgDialog.hide();
// When Http response code is '404'
if (statusCode == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if (statusCode == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else {
Toast.makeText(getApplicationContext(), "Posting failed", Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
And error occurs here: 这里出现错误:
else { Toast.makeText(getApplicationContext(), "Posting failed", Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
Screen Shoot Posting failed on application 屏幕拍摄在申请时发布失败
Screen Shoot Success on Postman 屏幕拍摄成功邮差
What Should I do to fix this problem? 我该怎么做才能解决这个问题? Thank you. 谢谢。
i tried to post the params as you wrote it and get this response 我试着在你写这篇文章时发布params并得到这个回复
400 Bad Request Show explanation Loading time: 645
Request headers
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: multipart/form-data
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Response headers
Date: Thu, 10 Dec 2015 08:30:07 GMT
Server: Apache
X-Request-Id: 8e80edd8-ff10-44f2-9235-6b8d307c179b
X-Runtime: 0.002612
X-Powered-By: Phusion Passenger 4.0.58
Content-Length: 38
Status: 400 Bad Request
Connection: close
Content-Type: application/json; charset=utf-8
if you tried to Make Get Connection at the link Like this http://openetizen.com/api/v1/articles?title=test&content=test its Will work fine 如果你试图在链接上建立连接像这样http://openetizen.com/api/v1/articles?title=test&content=test它会工作正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.