2017-03-08 21 views
-1

jsonをサーバーから取得してRecyclerViewに配置しようとしていますが、 "java.lang.String JSONObjectに変換できません "。私は私のjsonを追跡しようとしているが、何も表示されていない。何が間違っている、どうすればこれを解決することができますか?データの解析中にエラーが発生しました。org.json.JSONException:値が<html>で、java.lang.String型がJSONObjectに変換できません。

public class AsyncLoaderTask extends AsyncTask<Void, Void, Void> { 
    Context ctx; 
    `enter code here`ProgressDialog progressDialog; 
private static final String TAG = "AsyncLoaderTask"; 
String json_url="http://stocksearchapi.com/api/ 
api_key=43c30be222eb8b48f8d562f23b00e189bc42b0e9&search_text=CSO"; 
public AsyncLoaderTask(Context ctx) { 
    this.ctx = ctx; 
} 


@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    progressDialog= new ProgressDialog(ctx); 
    progressDialog.setIndeterminate(true); 
    progressDialog.setTitle("Please Wait.."); 
    progressDialog.setMessage("Download in Progress"); 
    progressDialog.setCancelable(false); 
    progressDialog.show(); 

} 



@Override 
protected Void doInBackground(Void... voids) { 

     try { 
      URL url = new URL(json_url); 
      HttpURLConnection httpURLConnection =(HttpURLConnection)url.openConnection(); 
      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
      StringBuilder stringBuilder = new StringBuilder(); 
      String line; 
      while ((line=bufferedReader.readLine())!=null){ 
       stringBuilder.append(line+"\n"); 
       Thread.sleep(500); 

      } 
      httpURLConnection.disconnect(); 



      String json_data= stringBuilder.toString().trim(); 

      JSONObject jsonObject= new JSONObject(json_data); 

      JSONArray jsonArray =jsonObject.getJSONArray("server_response"); 
      DatabaseHandler databaseHandler=new DatabaseHandler(ctx); 
      SQLiteDatabase db= databaseHandler.getWritableDatabase(); 

      int count =0; 
      while (count<jsonArray.length()) 
      { 

       JSONObject JO=jsonArray.getJSONObject(count); 
       count++; 
       databaseHandler.putInformation(JO.getString("company_name"),JO.getString("company_symbol"),db); 


      } 


     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 


    return null; 
} 

@Override 
protected void onPostExecute(Void aVoid) { 
    super.onPostExecute(aVoid); 
    progressDialog.dismiss(); 
} 
+0

を使用してJsonObjectをエンコード – Coder

答えて

0

NEW

response = [{"company_name": "Amazon.com, Inc.", "company_symbol": "AMZN", "listing_exchange": "NASDAQ"}] 

この応答は、その後、doInBackgroundでこの

JsonArray jsonArray = new JsonArray(response); 
JsonObject jsonObject = jsonArray.getJSONObject(0); 

//do with this object now 

OLD

リターンのような文字列操作を行う場合は()

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

    Log.i(TAG, "do in background"); 

    HttpURLConnection urlConnection; 
    try { 
     URL url = new URL(params[0]); 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     int statusCode = urlConnection.getResponseCode(); 

     // 200 represents HTTP OK 
     if (statusCode == 201 || statusCode == 200) { 

      BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
      StringBuilder response = new StringBuilder(); 
      String line; 
      while ((line = r.readLine()) != null) { 
       response.append(line); 
      } 
      ourresult = response.toString(); 
      return ourresult; 
     } 
     else if (statusCode == 400 || statusCode == 401 || statusCode == 404) { 

      JSONObject jsonObject=new JSONObject(); 
      jsonObject.put("Response",2); 
      ourresult=jsonObject.toString(); 
     } 
     else if(statusCode == 500) 
     { 
      JSONObject jsonObject=new JSONObject(); 
      jsonObject.put("Response",3); 
      ourresult=jsonObject.toString(); 
     } 
     else 
     { 
      JSONObject jsonObject=new JSONObject(); 
      jsonObject.put("Response",4); 
      ourresult=jsonObject.toString(); 
     } 
    } catch (Exception e) { 
      Log.e("Get", e.getLocalizedMessage()); 
    } 
    return ourresult; 
} 

そして、あなたの `json_data`の内容は何ですか?文字列

@Override 
protected void onPostExecute(String result) { 

    JSONObject json; 
    try { 
     json = new JSONObject(result); 

     //Handle Response 2,3,4 if any 
     .......... 
     //else 
     JSONArray jsonArray =json.getJSONArray("server_response"); 
     DatabaseHandler databaseHandler=new DatabaseHandler(ctx); 
     SQLiteDatabase db= databaseHandler.getWritableDatabase(); 

     do your stuff here... 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+0

同じエラー:値が、型がjava.lang.Stringの場合はJSONObjectに変換できません 取得したいJSONデータは次のようなものです。{{"company_name": "Amazon.com、Inc."、 "company_symbol ":" AMZN "、" listing_exchange ":" NASDAQ "}] –

関連する問題