如何做Json解析android中的发送消息

时间:2014-09-11 06:05:13

标签: android

public String sendMsg(String message) throws IOException, JSONException {

    try {

         sendmsgurl = "http://api.lociiapp.com/api/message/sendmessage?member_id="

                + 452

                + "&to_id="

                + 452

                + "&message="

                + "Travell Request"

                + "&Notificationtype="
                + "1"

                + "&notificationId="
                + "50011"
                + "&notify_status="
                + "false";
        ;

        Log.e("Valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", sendmsgurl);

        System.out.println("Valueeeeeeeeeeeeeeeeeee" + sendmsgurl);

    } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

    return jsonResp;

}


private String getResult(String response) {

    String test = "";

    try {

        test=JSONfunctions.getJSONfromURL(sendmsgurl);
        JSONObject _jobj = new JSONObject(test);

        test = _jobj.getString("responseCode");

    } catch (JSONException e) {

    }

    return test;

}

public class sendMesgTask extends AsyncTask<String, Void, Void> {

    String msg = "";

    @Override
    protected Void doInBackground(String... params) {

        // TODO Auto-generated method stub

        try {

            String result = sendMsg(params[0]);

            msg = getResult(result);

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (JSONException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return null;

    }

    @Override
    protected void onPostExecute(Void result) {

        if (msg.equals("1")) {

        } else {

        }

    }

}

并在On create中调用这样的方法:

new sendMesgTask().execute(Sendername
                        + ": Request For Travlling to You");

下面是Json功能代码:

public static String getJSONfromURL(String url){
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;


    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httppost = new HttpGet(url);
            httppost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml,application/json;q=0.9,/;q=0.8");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
    }

  //convert response to string
    try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }

    try{

        jArray = new JSONObject(result);            
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }

    return result;
}

当尝试运行此代码时,我得到Exception而不是获取repose代码 我得到的异常是错误:

Log-cat:

  1. &GT; 09-11 11:32:48.805:E / Valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee(15432):

      

    http://api.lociiapp.com/api/message/sendmessage?member_id=453&to_id=454&message=Travell   请求&安培; Notificationtype = 1&安培; notificationId = 50011&安培; notify_status =假       09-11 11:32:48.805:I / System.out(15432):Valueeeeeeeeeeeeeeeeeeehhttp://api.lociiapp.com/api/message/sendmessage?member_id = 453&amp; to_id = 454&amp; message = Travell   请求&安培; Notificationtype = 1&安培; notificationId = 50011&安培; notify_status =假       09-11 11:32:48.805:E / log_tag(15432):http连接错误java.lang.IllegalArgumentException:查询中的非法字符   索引87:    http://api.lociiapp.com/api/message/sendmessage?member_id=453&to_id=454&message=Travell   请求&安培; Notificationtype = 1&安培; notificationId = 50011&安培; notify_status =假       09-11 11:32:48.805:E / log_tag(15432):转换结果java.lang.NullPointerException时出错       09-11 11:32:48.805:E / log_tag(15432):解析数据时出错org.json.JSONException:

    的字符0输入结束
  2.   

1 个答案:

答案 0 :(得分:0)

您的网址参数中有空格字符。 “旅行要求”中的空格&#34;。之前使用URLEncoder.encode()。