POSTing JSON/XML using android-async-http (loopj)

前端 未结 9 786
广开言路
广开言路 2020-12-02 07:01

I am using android-async-http and really liking it. I\'ve run into a problem with POSTing data. I have to post data to the API in the following format: -

&l         


        
相关标签:
9条回答
  • 2020-12-02 07:31

    To post xml file to a php server :

    public class MainActivity extends AppCompatActivity {
    
    /**
     * Send xml file to server via asynchttpclient lib
     */
    
    Button button;
    String url = "http://xxx/index.php";
    String filePath = Environment.getExternalStorageDirectory()+"/Download/testUpload.xml";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        button = (Button)findViewById(R.id.button);
    
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postFile();
            }
        });
    }
    
    public void postFile(){
    
        Log.i("xml","Sending... ");
    
        RequestParams params = new RequestParams();
    
        try {
            params.put("key",new File(filePath));
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
    
        AsyncHttpClient client = new AsyncHttpClient();
    
        client.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes) {
                Log.i("xml","StatusCode : "+i);
            }
    
            @Override
            public void onFailure(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes, Throwable throwable) {
                Log.i("xml","Sending failed");
            }
    
            @Override
            public void onProgress(long bytesWritten, long totalSize) {
                Log.i("xml","Progress : "+bytesWritten);
            }
        });
    }
    

    }

    After adding android-async-http-1.4.9.jar to android studio, go to build.gradle and add : compile 'com.loopj.android:android-async-http:1.4.9' under dependencies

    And on AndroidManifest.xml add:

    <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    0 讨论(0)
  • 2020-12-02 07:37

    Loopj POST examples - extended from their Twitter example:

    private static AsyncHttpClient client = new AsyncHttpClient();
    

    To post normally via RequestParams:

    RequestParams params = new RequestParams();
    params.put("notes", "Test api support"); 
    client.post(restApiUrl, params, responseHandler);
    

    To post JSON:

    JSONObject jsonParams = new JSONObject();
    jsonParams.put("notes", "Test api support");
    StringEntity entity = new StringEntity(jsonParams.toString());
    client.post(context, restApiUrl, entity, "application/json",
        responseHandler);
    
    0 讨论(0)
  • 2020-12-02 07:37

    To post XML

    protected void makePost() {
        AsyncHttpClient client = new AsyncHttpClient();
        Context context = this.getApplicationContext();
        String  url = URL_String;
        String  xml = XML-String;
        HttpEntity entity;
        try {
            entity = new StringEntity(xml, "UTF-8");
        } catch (IllegalArgumentException e) {
            Log.d("HTTP", "StringEntity: IllegalArgumentException");
            return;
        } catch (UnsupportedEncodingException e) {
            Log.d("HTTP", "StringEntity: UnsupportedEncodingException");
            return;
        }
        String  contentType = "string/xml;UTF-8";
    
        Log.d("HTTP", "Post...");
        client.post( context, url, entity, contentType, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                Log.d("HTTP", "onSuccess: " + response);
            }
              ... other handlers
        });
    }
    
    0 讨论(0)
提交回复
热议问题