来自真实Android设备的POST请求

时间:2014-08-11 18:31:08

标签: android

我正在研究将其位置发布到服务器的android应用程序。这是一个问题。

当我在genymotion中运行此应用程序时,它会成功将POST请求发送到服务器。但是,当我在三星Galaxy笔记上安装时,它并没有发送这些帖子请求。

问题出在哪里?

编辑:我已成功通过GET请求从互联网上获取数据

代码

class Posting extends AsyncTask<Location, String, String>{
    @Override
    protected void onPreExecute(){

    }

    @Override
    protected String doInBackground(Location... locations){
    RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint(RetrofitClient.API_URL2).build();

    RetrofitClient.TestRetro retrofit = restAdapter.create(RetrofitClient.TestRetro.class);
    IssdDeviceLog body=new IssdDeviceLog();
    body.setDeviceNo("hello from SAMSUNG MAIN ACTIVITY service");
    body.setLatitude(new BigDecimal(locations[0].getLatitude()));
    body.setLongtitude(new BigDecimal(locations[0].getLongitude()));
    Date date= new Date();
    body.setDate(date.toString());
    Gson gson = new Gson();
    String json = gson.toJson(body);
    Response response=retrofit.sendLocation(json);
    Log.d("status code","   "+response.getStatus());
    return null;
    }

    @Override
    protected void onPostExecute(String file_url) {

    }
    }

关于

1 个答案:

答案 0 :(得分:3)

如果您收到任何错误,您可以提出错误吗?或者您可以使用排球库我正在分享此代码您可以将此用于您的帖子请求

public class send_data extends AsyncTask<String, String, String> {
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(main.this);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setTitle("please wait...");
        mProgressDialog.show();
    }
    protected String doInBackground(String... args) {

        String url = "http://your_url";
        try {
            ArrayList<BasicNameValuePair> nvp = new ArrayList<BasicNameValuePair>(
                    1);
            nvp.add(new BasicNameValuePair("key for your data","yourdata"));


            String str_responsebody = obj_service.executer(url, nvp);
            Log.i("responce", str_responsebody + "===");
            return str_responsebody;

        } catch (Exception e) {

            Log.i("error1", "" + e.toString());
            return null;
        }

    }

    protected void onPostExecute(String result) {

        super.onPostExecute(result);



        try {

            Log.i("result", result);
                mProgressDialog.dismiss();


                }


        } catch (Exception e) {
            Log.e("error2", "" + e.toString());
            e.printStackTrace();
            mProgressDialog.dismiss();
        }
    }
}

下载volley lib并添加到您的项目中。教程[link]:http://www.androidhive.info/2014/05/android-working-with-volley-library-1/