无法从Android发布/上传数据到服务器

时间:2015-12-10 07:53:18

标签: java android api loopj

我对此感到困惑,我不明白问题的来源。使用Postman成功测试了API,但在我的应用程序上失败了。

我的代码是:

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

我该怎么做才能解决这个问题?谢谢。

1 个答案:

答案 0 :(得分:0)

我试图在你写这篇文章时发布params并得到这个回复

users

如果你试图在链接上建立连接像这样 http://openetizen.com/api/v1/articles?title=test&content=test 它的意思很好 enter image description here