Android上传数据至服务器失败

3

我卡在这了,不明白问题出在哪里。API在Postman中测试成功,但在我的应用程序中失败。

我的代码如下:

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();
            }
        }

这里发生了一个错误:

 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();
            }

应用程序发布失败截图

Postman发布成功截图

请问我该怎么做来解决这个问题?谢谢。


2
你确定它是一个“Post”类型的Web服务吗?检查一下,我认为它是一个“Get”类型的服务。 - santoXme
我对此不是很了解,但我认为你应该在onFailure()方法中记录Throwable.getMessage()并发布它,这样人们可能会提供更多帮助。 - JRowan
1个回答

0

我尝试按照你所写的参数进行提交,但得到了以下响应:

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 

如果您尝试在链接中进行Get连接,如下所示: http://openetizen.com/api/v1/articles?title=test&content=test 它将正常工作 enter image description here


当然,如果使用HTTP GET方法就可以正常工作,但我想使用POST仍然卡住了。 - cevysays
这是一个服务问题,Web服务允许您获取而不是发布。要解决此问题,请将其更改为“允许发布”。 - S. Alawadi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接