我被困在这件事上了,我不明白问题从何而来。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();
            }

Screen Shoot Posting failed on application
Screen Shoot Success on Postman
我该怎么做才能解决这个问题?谢谢您。

最佳答案

我试着在你写的时候贴上这些参数,然后得到这个回复

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

如果你想在这个链接上建立连接
http://openetizen.com/api/v1/articles?title=test&content=test
它的作用很好
java - 无法将数据从Android发布/上传到服务器-LMLPHP

关于java - 无法将数据从Android发布/上传到服务器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34196407/

10-11 08:05