2014-09-11 56 views
0
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 { 

     } 

    } 

} 

發送的messge Json的解析和這樣的調用方法上創建:怎麼辦請在Android

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

是Json的funcation代碼:

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; 
} 

當嘗試運行此代碼然後我得到異常,沒有得到休息碼 和異常下面我列舉得到的是錯誤:

日誌貓:

  1. > 11月9日至11日:32:48.805:E/Valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee(15432):

    http://api.lociiapp.com/api/message/sendmessage?member_id=453&to_id=454&message=Travell Request&Notificationtype=1&notificationId=50011&notify_status=false 09-11 11:32:48.805: I/System.out(15432): Valueeeeeeeeeeeeeeeeeeehttp://api.lociiapp.com/api/message/sendmessage?member_id=453&to_id=454&message=Travell Request&Notificationtype=1&notificationId=50011&notify_status=false 09-11 11:32:48.805: E/log_tag(15432): Error in http connection java.lang.IllegalArgumentException: Illegal character in query at index 87: http://api.lociiapp.com/api/message/sendmessage?member_id=453&to_id=454&message=Travell Request&Notificationtype=1&notificationId=50011&notify_status=false 09-11 11:32:48.805: E/log_tag(15432): Error converting result java.lang.NullPointerException 09-11 11:32:48.805: E/log_tag(15432): Error parsing data org.json.JSONException: End of input at character 0 of

+0

錯誤告訴所有在查詢參數中有一個非法字符在87. – greenapps 2014-09-11 06:09:29

+0

http://api.lociiapp.com/api/消息/ SendMessage函數?member_id = 453&to_id = 454&消息=特拉維爾請求&Notificationtype = 1&notificationId = 50011&notify_status =假看到這項工作很好,當我在Broser – Edge 2014-09-11 06:10:47

+1

是把由於瀏覽器知道如何發送的空間。它WIL URL發送。所有的空間之前編碼字符串將由20%被替換 – greenapps 2014-09-11 06:13:42

回答

0

。在你的URL參數的空格字符。 「旅行請求」中的空格,使用URLEncoder.encode()之前

+0

還是同樣的問題,我已經添加\t測試= JSONfunctions.getJSONfromURL(Uri.encode(sendmsgurl)); \t \t \t的JSONObject _jobj =新的JSONObject(測試); – Edge 2014-09-11 06:18:38

+0

你在哪裏使用URLEncoder.encode ()? – greenapps 2014-09-11 06:20:27

+0

請告訴如何編碼或如何替換空格請建議 – Edge 2014-09-11 06:20:59