通过JSON将数据发送到服务器URL

时间:2017-10-16 02:17:57

标签: java android android-asynctask

嗨,我是AsyncTask的新手,我需要将数据发送到API服务器。我正在做连接,我被困在这里。我读到了AsyncTask,这是我见过的代码。首先,如果我确定设备是否已连接,它将在给定的URL上发送数据,否则。它将通过短信发送

public class SendData extends AsyncTask <String, Void, Boolean> {
    DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");
    Date date = new Date();
    String datefinal = dateFormat.format(date).toString();
    String url = "http://192.168.1.212/mobile_alerts_api.php?location=&msg=&datetime=&id=";


    @Override
    protected Boolean doInBackground(String... urls) {
        try{
            HttpGet httppost = new HttpGet(url);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsono = new JSONObject(data);
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {

            e.printStackTrace();
        }
        return false;
    }
    protected void onPostExecute(Boolean result) {

    }
}

2 个答案:

答案 0 :(得分:0)

使用:

HttpPost httppost = new HttpPost(url);

而不是

HttpGet httppost = new HttpGet(url);

Get用于从服务器获取数据。 Post用于将数据发送到服务器

答案 1 :(得分:0)

results=sapply(E(g), function(i){
  a = ends(g, i)[1]
  b = ends(g, i)[2]
  source_neighbors = neighbors(g, a)
  target_neighbors = neighbors(g, b)
  num_overlap_neighbors = length(intersection(source_neighbors, target_neighbors))

  return(num_overlap_neighbors)
})
print(results)

然后在异步任务类中执行httppost

相关问题