duanne9313 2014-10-12 02:43
浏览 41

json post请求java不向服务器发送数据

I try to Create a class which is unique to all json request and try to send json request from it to server. It takes only request url and json StringEntity only. Request send but problem is when i try to access data from server can't find that post data.

JSONClinet.java

package info.itranfuzz.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class JSONClient {

    private final HttpClient httpClient;
    private HttpPost httpPost;
    private HttpResponse httpResponse;

    public JSONClient() {
        httpClient = new DefaultHttpClient();
    }

    public String doPost(String url, StringEntity se) {

        InputStream inputStream = null;
        String result = "";

        httpPost = new HttpPost(url);

        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        httpPost.setEntity(se);
        try {
            httpResponse = httpClient.execute(httpPost);
            inputStream = httpResponse.getEntity().getContent();
            if (inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";
        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }
        return result;
    }

    private static String convertInputStreamToString(InputStream inputStream)
            throws IOException {
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while ((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;

    }

}

Server accss code is here. There email and lat and lng to send to server.

<?php

//set content type to json
        header('Content-type: application/json');

        $email = $this->input->post("email");
        $lat = $this->input->post('lat');
        $lng = $this->input->post('lng');

        $status = array("STATUS"=>"false");
        if($this->donor->updateLocationByEmail($email,$lat,$lng)){
            $status = array("STATUS"=>"true");
        }
        array_push($status, array("email"=>$email,"lat"=>$lat,"lng"=>$lng));
        echo json_encode($status);

?>

My calling method is this

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        JSONClient jClient = new JSONClient();

        Location loc = new Location(LocationService.this);
        LatLng p = loc.getLocation();

        if (p != null) {
            String json = "";

            try {
                JSONObject jsonObject = new JSONObject();
                // jsonObject.accumulate("email", WebLoad.STORE.getEmail());
                jsonObject.put("email", "b@gmail.com");
                jsonObject.put("lat", p.getLat());
                jsonObject.put("lng", p.getLng());

                json = jsonObject.toString();

                System.out.println(jClient.doPost(WebLoad.ROOTURL
                        + "/donor_controller/updatelocation", new StringEntity(
                        json)));

            } catch (JSONException e) {
                System.out.println("Json exception occur");
            } catch (UnsupportedEncodingException e) {
                System.out.println("Unsupported ecodding exception occur");
            }

        }
        return super.onStartCommand(intent, flags, startId);
    }
  • 写回答

2条回答 默认 最新

  • dsrjs86444 2014-10-12 03:13
    关注
    $email = $this->input->$_POST('email')
    

    use this way any try to do . change post to $_POST['variable name']. as i tell this , you write to server side PHP. in PHP get and post method we access $_GET and $_POST.

    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器