2016-04-19 9 views
0

アンドロイドプロジェクトで作業していて、JSONを使用してサーバーとデータを交換しています。JSONファイルをUTF-8形式で使用すると問題が発生します。アプリが停止し、私がしないとき、それは正常に動作します。 私は本当に正しいcharectersを持つために私のファイルをUTF-8形式にする必要があります。 どこに行方がありませんか?進んでいただきありがとうございます。ここでandroidで使用されるUTF-8 JSONファイルの問題

は私のサービス・ハンドラクラスです:

パブリッククラスServiceHandler {

static String response = null; 
public final static int GET = 1; 
public final static int POST = 2; 

public ServiceHandler() { 
} 

/** 
* Making service call 
* @url - url to make request 
* @method - http request method 
* */ 
public String makeServiceCall(String url, int method) { 
    return this.makeServiceCall(url, method, null); 
} 

/** 
* Making service call 
* @url - url to make request 
* @method - http request method 
* @params - http request params 
* */ 
public String makeServiceCall(String url, int method, List<NameValuePair> params) { 
    try { 
     // http client 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpEntity httpEntity = null; 
     HttpResponse httpResponse = null; 

     // Checking http request method type 
     if (method == POST) { 
      HttpPost httpPost = new HttpPost(url); 
      // adding post params 
      if (params != null) { 
       httpPost.setEntity(new UrlEncodedFormEntity(params)); 
      } 

      httpResponse = httpClient.execute(httpPost); 

     } else if (method == GET) { 
      // appending params to url 
      if (params != null) { 
       String paramString = URLEncodedUtils.format(params, "utf-8"); 
       url += "?" + paramString; 
      } 
      HttpGet httpGet = new HttpGet(url); 
      httpResponse = httpClient.execute(httpGet); 
     } 
     httpEntity = httpResponse.getEntity(); 
     response = EntityUtils.toString(httpEntity); 

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

    return response; 

} 

。ここでは、私が主な活動で使用する方法である:

プライベートクラスGetContacts AsyncTask {

を拡張します
@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Showing progress dialog 
     pDialog = new ProgressDialog(SecondActivity.this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // Creating service handler class instance 
     ServiceHandler sh = new ServiceHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

     Log.d("Response: ", "> " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = null; 
       try { 
        jsonObj = new JSONObject(jsonStr); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

       // Getting JSON Array node 
       if (jsonObj != null) { 
        contacts = jsonObj.getJSONArray(TAG_CONTACTS); 
       } 


       // looping through All Contacts 
       for (int i = 0; i < contacts.length(); i++) { 
        JSONObject c = contacts.getJSONObject(i); 

        String id = c.getString(TAG_ID); 
        String title = c.getString(TAG_Title); 
        String content = c.getString(TAG_CONTENT); 
        String date = c.getString(TAG_DATE); 



        // tmp hashmap for single contact 
        HashMap<String, String> contact = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        contact.put(TAG_ID, id); 
        contact.put(TAG_Title, title); 
        contact.put(TAG_CONTENT, content); 
        contact.put(TAG_DATE, date); 

        // adding contact to contact list 
        conatctlist.add(contact); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.e("ServiceHandler", "Couldn't get any data from the url"); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     ListView lv = (ListView)findViewById(R.id.listView); 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       SecondActivity.this, contactlist, 
       R.layout.list_item_2, new String[] { TAG_Title, TAG_CONTENT, 
       TAG_DATE }, new int[] { R.id.name, 
       R.id.email, R.id.date }); 

     lv.setAdapter(adapter); 

    } 
}//end getContacts 
+1

_someクラッシュが発生し、アプリが停止します - スタックトレースを投稿すると、何がうまくいかないのかを推測するのが本当に難しいです。 – 1615903

答えて

0

あなたはこのようにHttpClientを定義する必要があります... (テストされていません)

HttpParams params = new BasicHttpParams(); 
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
HttpProtocolParams.setContentCharset(params, "UTF-8"); 
params.setBooleanParameter("http.protocol.expect-continue", false); 
HttpClient httpclient = new DefaultHttpClient(params); 
関連する問題