2016-09-19 2 views
-4

JSON 게시물 데이터를 WebView에 보내면 원시 데이터로 webservice를 호출 할 수 있습니까? json in posturl webview

내 코드,

fb.loadData(base64, "text/html; charset=UTF-8", "base64"); 
+1

webservice를 호출하기 위해 WebView를 사용하는 요점은 무엇입니까? – Selvin

답변

1

가 있다고 가정 해보십시오입니다 이것은 당신의 JSON입니다 : 1.

ReqBody : [{ 
                                'LoginId':'LoginId',                       
                                'pass' :'pass'                       
                            } 
                        ] 

귀하의 POJO 클래스는 다음과 같습니다

public class ExamplePojo{ 
     @SerializedName("LoginId") 
     private String LoginId; 
     @SerializedName("pass") 
     private String Password; 

     //getter setter method 
      } 

2. Gson을 사용하면 지정된 객체를 해당 Json 표현으로 직렬화합니다. 당신은 키 값 쌍에 데이터를 전송해야 4.Suppose

public static String getJsonString(Object obj) throws JSONException { 
     Gson gson = new Gson(); 
     if (obj != null) { 
      String json = gson.toJson(obj); 
      JSONObject jsonObject = new JSONObject(json); 


      return jsonObject.toString(); 
     } else 
      return ""; 
    } 

3.Post this data(Return by getJsonString() say dataToPost) as per your requirement 

다음 다음을 추가합니다

NameValuePair dataToSend = new NameValuePair("key", dataToPost); 
postData = getQuery(dataToSend); 



private String getQuery(NameValuePair params) throws UnsupportedEncodingException { 
     StringBuilder result = new StringBuilder(); 

     result.append(URLEncoder.encode(params.getKey(), "UTF-8")); 
     result.append("="); 
     result.append(URLEncoder.encode(params.getValue(), "UTF-8")); 
//  } 

     return result.toString(); 
    } 

try { 
     mWebView.postUrl(URL, postData.getBytes("UTF-8")); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 


    OR 



URL url; 
      String response = ""; 
      try { 
       url = new URL(URL); 

       HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
       conn.setReadTimeout(TIMEOUT); 
       conn.setConnectTimeout(TIMEOUT); 
       conn.setRequestMethod("POST"); 
       conn.setChunkedStreamingMode(0); 
       conn.setDoInput(true); 
       conn.setDoOutput(true); 


       OutputStream os = conn.getOutputStream(); 
       BufferedWriter writer = new BufferedWriter(
         new OutputStreamWriter(os, "UTF-8")); 
       writer.write(dataToSend); 

       writer.flush(); 
       writer.close(); 
       os.close(); 
       int responseCode = conn.getResponseCode(); 

       if (responseCode == HttpsURLConnection.HTTP_OK) { 
        String line; 
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
        while ((line = br.readLine()) != null) { 
         response += line; 
        } 
       } else { 
        response = ""; 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return response; 

응답은 다음과 같은 웹보기에 게시 할 수있는 HTML 페이지가됩니다 : mWebView.loadData을 (응답 , "text/html", "utf-8");

+0

그 오류가 발생했습니다 허용되지 않는 키 문자들 –

+0

오류에 대해 더 자세히 설명해 주시겠습니까? –

+0

나는 webview.posturl()을 사용하고 열려있는 webview 다음에 사용 금지 키 문자 –

0

webView.postUrl("url", data.getBytes("UTF-8")); 
+0

자세한 내용을 편집하십시오. 코드 전용 및 "시도하십시오"답변은 검색 가능한 콘텐츠가 없으므로 권장하지 않으며 누군가가 "시도해"야하는 이유를 설명하지 않습니다. – abarisone

+0

좋아요. 답변을 설명하고 좋은 조언을 해 주셔서 감사드립니다. –