Using Post in Retrofit

时间:2015-08-06 13:54:33

标签: java android

Can you anyone help me giving good example on how to use retrofit for posting large data from my local DB to mysql server.

Currently I am using async-http API and strangely there is always memory error coming up. I am looking for better API that wont give memory error while uploading huge text of data.

This is my setup:

List<TextDetails> unTextList = dbvalue.getTextData();
    for (TextDetails td : unTextList)
    {
    String textID = td.getSerialNumber();
    String textSMS = td.getText();
    String textAddress = td.getFulladdress();
    String textDate = td.getFulldate();
    String textException = td.getExceptiontext();


    textDetailsBackUpDataOnline(textID , textSMS, textAddress, textDate, textException);
    }

    private void textDetailsBackUpDataOnline(final String textID ,
    String textSMS, String textAddress, String textDate, String textException)
    {
    final String uploadWebsite = url_backup_text_details;

    RequestParams requestParams = new RequestParams();

    requestParams.put("textSMS", textSMS);
    requestParams.put("textAddress", textAddress);
    requestParams.put("textDate", textDate);
    requestParams.put("textException", textException);

    Text_HttpClient.post(uploadWebsite, requestParams, new AsyncHttpResponseHandler()
    {
    @Override
    public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
    {
    Log.e("textID", "= how many times");
    }

    @Override
    public void onFailure(int statusCode, org.apache.http.Header[] headers, byte[] errorResponse, Throwable e)
    {
    e.printStackTrace(System.out);
    }
    });
    }

Text_HttpClient class has the following:

public class Text_HttpClient 
{
private static AsyncHttpClient client = new AsyncHttpClient();

public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler)
{
client.get(url, params, responseHandler);
}

public static void post(String url, RequestParams requestParams, AsyncHttpResponseHandler responseHandler)
{
client.post(url, requestParams, responseHandler);
}

}

1 个答案:

答案 0 :(得分:1)

1)编写服务接口:

t

这里我的Rest服务需要3个参数,根据需要进行更改。

2)编写POJO以将从Rest Api返回的JSON转换为java类对象,以便您可以使用数据。

只需将您的JSON复制到this site,选择JSON源类型,注释为Gson。它会自动为您的JSON生成POJO。

3)在您的主要活动

Object