2017-12-21 54 views
0

HttpPostを使用してファイルをアップロードしようとしています。phpに送信します。回答はJsonArrayになります。しかし、私が抱えている問題は、JsonArrayまたはオブジェクトをアンドロイドアプリで使用することです。配列の情報は、アプリでSQLite dbに格納されます。私はJSONArray ARR = object.getJSONArray(「応答」)の「 JSONArrayに変換できないタイプorg.json.JSONObjectの応答に」配列から、次のエラーのレスポンスでorg.json.JSONObjectをJSONArrayに変換できません

を取得するデータを抽出することができません。

私はHttpPostとうまくあいまいではありません。私はネットからいくつかのものを試しましたが、成功しませんでした。

Array 
    ( 
    [response] => Array 
     ( 
      [Filepathserver] => webaddress 
      [email] => [email protected] 
      [syncstatus] => yes 
     ) 

) 

javaファイル

String url = "uploadphpfile.php"; 
    File file = new File(filepath); 
    try { 

     HttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httppost = new HttpPost(url); 

     httppost.setEntity(mpEntity); 
     Log.d("enitity",mpEntity.toString()); 

     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity r_entity = response.getEntity(); 

     String result = EntityUtils.toString(r_entity); 
     JSONObject object = new JSONObject(result); 

     JSONArray arr =object.getJSONArray("response"); 

     for (int i = 0; i < arr.length(); i++) { 
      JSONObject obj = arr.getJSONObject(i); 
      Log.d("filepathserver",obj.get("Filepathserver").toString()); 
     } 
     //Do something with response... 
     int statusCode = response.getStatusLine().getStatusCode(); 
     if (statusCode == 200) { 
      // Server response 
      String responseString = EntityUtils.toString(r_entity); 
     } else { 
      String responseString = "Error occurred! Http Status Code: " 
        + statusCode; 
     } 
    } catch (Exception e) { 
      Log.e(DBHelperReports.class.getName(), e.getLocalizedMessage(), 
    e); 
    } 
    } 

PHPファイル

$response = array(); 
    $response["Filepathserver"] = $target_path; 
    $response["email"] = $target_path; 
    $response["syncstatus"] = 'yes'; 
    echo json_encode(array("response"=>$response)); 

Logcat

Value 
{"Filepathserver":"webaddress","email":"[email protected]","syncstatus":"yes"} 
at response of type org.json.JSONObject cannot be converted to JSONArray 

org.json.JSONException: Value 
{"Filepathserver":"webaddress","email":"[email protected]","syncstatus":"yes"} 
at response of type org.json.JSONObject cannot be converted to JSONArray 

at org.json.JSON.typeMismatch(JSON.java:100) 

at org.json.JSONObject.getJSONArray(JSONObject.java:588) 

at com.multiinspectionelite.DBHelperReports$1.run(DBHelperReports.java:383) 

at java.lang.Thread.run(Thread.java:762) 
+0

タイプの不一致エラー。返信いただきありがとうございます。 –

答えて

3

あなたのPHPファイルはJSONオブジェクトではなく、JSON配列を生成します。そう

、ちなみにJSONObject arr =object.getJSONObject("response");

を試してみてください、あなたのlogcatはすでにJSONObjectとしてそれを処理するためにあなたを思い出していました。

+0

私は、例えば、JSONオブジェクト – Ahsan

+0

内JSONArrayことができ、それがオブジェクトであることを理解どのように: { "動物":{ "犬":[ { "名": "スヌーピー"、 "色": "ネコ " 、 } "1":[ { "名前": "キティ"、 "色": "白"、 "年齢": "1": "" 年齢「 、" 白 }、 { "text": "Garfield"、 "color": "orange"、 "age": "2" } ] } } 'animals'は' JSONObject'です。 2つの 'JSONArray'を含んでいます。 'cats'と' dogs'は 'JSONArray'です。 'cats'や' dogs'には 'JSONObject'があります。 –

関連する問題