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

json post请求java不向服务器发送数据,java,php,android,json,Java,Php,Android,Json,我尝试创建一个对所有json请求都是唯一的类,并尝试将json请求从该类发送到服务器。它只接受请求url和json字符串实体。请求发送,但问题是当我试图从服务器访问数据时,找不到post数据 JSONClinet.java package info.itranfuzz.service; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.Inp

我尝试创建一个对所有json请求都是唯一的类,并尝试将json请求从该类发送到服务器。它只接受请求url和json字符串实体。请求发送,但问题是当我试图从服务器访问数据时,找不到post数据

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;

    }

}
服务器访问代码在这里。 有电子邮件、lat和lng发送到服务器

<?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);

?>
用这种方法做任何尝试。将post更改为$\u post['variable name']。正如我所说的,您可以编写服务器端PHP。在PHP get和post方法中,我们访问$\u get和$\u post。

//导入包
//import packages


public class DBConnection {

static InputStream is = null;
static JSONObject jsonObject = null;
static String json = "";

// This is a constructor of this class
public DBConnection() {

}

/*
 * function get jsonObject from URL by making HTTP POST or GET method.
 */

public JSONObject createHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request

    try {
        // check for request method
        if (method == "POST") {
            // request method is POST and making default client.
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } else if (method == "GET") {
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
    } catch (ClientProtocolException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
        Log.w("Error" ,"My Error" + json);
    } catch (Exception ex) {
        Log.e("Buffer Error", "Error converting result " + ex.toString());
    }

    // try to parse the string to a JOSN object
    try {

        Log.w("sub",json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
        jsonObject = new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));

    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return new object
    return jsonObject;

}
公共类数据库连接{ 静态InputStream为空; 静态JSONObject JSONObject=null; 静态字符串json=“”; //这是此类的构造函数 公共数据库连接(){ } /* *函数通过生成HTTP POST或get方法从URL获取jsonObject。 */ 公共JSONObject createHttpRequest(字符串url、字符串方法、, 列表参数){ //发出HTTP请求 试一试{ //检查请求方法 如果(方法==“POST”){ //请求方法是POST并生成默认客户端。 DefaultHttpClient httpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(url); setEntity(新的UrlEncodedFormEntity(参数)); HttpResponse HttpResponse=httpClient.execute(httpPost); HttpEntity HttpEntity=httpResponse.getEntity(); is=httpEntity.getContent(); }else if(方法==“GET”){ //请求方法是GET DefaultHttpClient httpClient=新的DefaultHttpClient(); String paramString=URLEncodedUtils.format(params,“utf-8”); url+=“?”+参数字符串; HttpGet HttpGet=新的HttpGet(url); HttpResponse HttpResponse=httpClient.execute(httpGet); HttpEntity HttpEntity=httpResponse.getEntity(); is=httpEntity.getContent(); } }捕获(不支持DencodingException ex){ 例如printStackTrace(); }捕获(客户端协议例外){ 例如printStackTrace(); }捕获(IOEX异常){ 例如printStackTrace(); } 试一试{ BufferedReader reader=新的BufferedReader(新的InputStreamReader( is,“iso-8859-1”),8); StringBuilder sb=新的StringBuilder(); 字符串行=null; 而((line=reader.readLine())!=null){ sb.追加(第+行“\n”); } is.close(); json=sb.toString(); Log.w(“错误”、“我的错误”+json); }捕获(例外情况除外){ Log.e(“缓冲区错误”,“错误转换结果”+ex.toString()); } //尝试将字符串解析为JOSN对象 试一试{ Log.w(“sub”,json.substring(json.indexOf(“{”),json.lastIndexOf(“}”)+1); jsonObject=newJSONObject(json.substring(json.indexOf(“{”)、json.lastIndexOf(“}”)+1); }捕获(JSONException e){ Log.e(“JSON解析器”,“错误解析数据”+e.toString()); } //返回新对象 返回jsonObject; }
}


请,您尝试以这种方式提出请求。

@Isuru请使用[]括号>$\u POST['email']。并检查您是否通过了正确的url。$\u POST['email']我已经尝试过了,但同样的@uma抱歉。。。。。。。。。!我不知道。但是你把url.php扩展名(WebLoad.ROOTURL+“/provider\u controller/updatelocation.php”)放在上面可能会成功。(WebLoad.ROOTURL+“/provider\u controller/updatelocation.php”)没有什么,因为我使用了codeginter,它忽略了php扩展名!谢谢你:)
$email = $this->input->$_POST('email')
//import packages


public class DBConnection {

static InputStream is = null;
static JSONObject jsonObject = null;
static String json = "";

// This is a constructor of this class
public DBConnection() {

}

/*
 * function get jsonObject from URL by making HTTP POST or GET method.
 */

public JSONObject createHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request

    try {
        // check for request method
        if (method == "POST") {
            // request method is POST and making default client.
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } else if (method == "GET") {
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
    } catch (ClientProtocolException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
        Log.w("Error" ,"My Error" + json);
    } catch (Exception ex) {
        Log.e("Buffer Error", "Error converting result " + ex.toString());
    }

    // try to parse the string to a JOSN object
    try {

        Log.w("sub",json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
        jsonObject = new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));

    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return new object
    return jsonObject;

}