Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将字符串转换为JSONObject_Php_Android_Json - Fatal编程技术网

Php 将字符串转换为JSONObject

Php 将字符串转换为JSONObject,php,android,json,Php,Android,Json,我试图将数据从android应用程序发送到服务器,但当我尝试运行应用程序时,它会连接到服务器,但过了一段时间,一个错误停止了我的应用程序 错误是: “05-04 19:39:36.401:E/JSON解析器(2323):解析数据org.JSON.JSONException:Value时出错abc@yahoo.com无法将java.lang.String类型的转换为JSONObject“ 在下面的代码中。。。。请检查最后一个try catch。。。。 如果有人有任何想法,请帮助 public cl

我试图将数据从android应用程序发送到服务器,但当我尝试运行应用程序时,它会连接到服务器,但过了一段时间,一个错误停止了我的应用程序

错误是:

“05-04 19:39:36.401:E/JSON解析器(2323):解析数据org.JSON.JSONException:Value时出错abc@yahoo.com无法将java.lang.String类型的转换为JSONObject“

在下面的代码中。。。。请检查最后一个
try catch
。。。。 如果有人有任何想法,请帮助

public class JSONParser {static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            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 e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.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();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
公共类JSONParser{static InputStream is=null;
静态JSONObject jObj=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
//函数从url获取json
//通过使用HTTP POST或GET方法
公共JSONObject makeHttpRequest(字符串url、字符串方法、,
列表参数){
//发出HTTP请求
试一试{
//检查请求方法
如果(方法==“POST”){
//请求方法为POST
//defaultHttpClient
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();
}           
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 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();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
}

}

可能是因为您正在尝试转换字符串“abc@yahoo.com“给JSONObject。您需要字符串为JSON格式。比如:

{
    "email":"abc@yahoo.com"
}

如果您正在读取一个简单的字符串值,例如电子邮件,那么让我们说abc@yahoo.com,则应首先创建JSONObject,并在其中设置键/值对

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();
    **jObj = new JSONObject();**
    **jObj.putString("email", json);**

} catch (Exception e) {
    Log.e("Buffer Error", "Error converting result " + e.toString());
}
答案用部分原始代码编辑 现在,您已经创建了自己的JSONObject,以防服务器不返回JSONObject。
希望有帮助。

您能发布从服务器上获得的信息吗?当前代码看起来没有错;添加(新的BasicNameValuePair(“用户名”,signupusernameString));添加(新的BasicNameValuePair(“电子邮件”,signupemailString));添加(新的BasicNameValuePair(“密码”,signuppasswordString));//注意,创建产品url接受POST方法JSONObject json=jsonParser.makeHttpRequest(url\u create\u signup,“POST”,params);用这个我传递电子邮件…我指的是你从服务器得到的回复。如果是这样的话abc@yahoo.com,它应该是{“我的钥匙”:abc@yahoo.com“}所以它变成了一个JSONObject。请参阅编辑后的答案,在这里,当您应该阅读流并修改它时,我重用了该部分。