简体   繁体   English

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

[英]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.

相关问题 将注册数据从Android上传到PHP服务器 - To upload Registration data from Android To PHP server 将数据发布到android中的php服务器 - Post data to php server in android 无法将zip文件从Android上传到服务器sendto失败:Epipe(管道断开) - Unable to upload zip file from Android to server sendto failed: epipe (broken pipe) 如何将数据发布到Android中的服务器? - How to post the data to server in android? 将表单数据发布到服务器(Android) - Post form data to Server(Android) 如何在post方法中将数据从android应用发送到服务器? - how to send data from an android app to a server in the post method? 如何从前端服务器发送文件到后端服务器(用于上传),该服务器接受来自客户端浏览器的发布请求(表单数据 - >文件输入)? - How to Send a file to backend server (for upload) from frontend server which is accepting post request from client browser (form data -> file input)? 在将数据上传到android中的服务器之前对数据进行身份验证 - Authenticate data before upload on server in android Android多图像上传无法将数据发送到服务器 - Android Multiple image upload not send data to server Android无法将多种类型的文件上传到PHP服务器 - Android failed to upload multiple type of files to PHP server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM