JSON リクエストに問題があります。API を読み取って、このコード (以下) で解析しようとしています。これを取得したら、設定する必要があります。私が得ているエラーはログの下にあります。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class jsonParser {
//initialize
static InputStream stream = null;
static String result = " ";
static JSONObject object =null;
public jsonParser(){
}
public JSONObject getJSONfromURL (String url){
//HTTP call
try{
URLConnection connection = new URL(url).openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()), 2048 * 16);
StringBuffer builder = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
String blah = builder.toString();
JSONObject jsonObj = new JSONObject(new String(blah));
JSONArray data = jsonObj.getJSONArray("Categories");
for(int i=0; i < data.length(); i++)
{
object = data.getJSONObject(i);
String id = object.getString("id");
String value = object.getString("name");
Log.d("Item name: ", value);
}
} catch(Exception e){
Log.e("Http Error","Error in http connection " + e.toString());
}
return object;
}
}
以下のエラーログ
05-12 02:49:44.553: E/Http Error(19288): Error in http connection org.json.JSONException: Value ["Categories",[[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]]] of type org.json.JSONArray cannot be converted to JSONObject
05-12 02:49:44.553: W/dalvikvm(19288): threadid=1: thread exiting with uncaught exception (group=0x40fb6438)
05-12 02:49:44.563: E/AndroidRuntime(19288): FATAL EXCEPTION: main
05-12 02:49:44.563: E/AndroidRuntime(19288): java.lang.NullPointerException
05-12 02:49:44.563: E/AndroidRuntime(19288): at com.example.hstnc_activity.DisplayServiceActivity$Request.onPostExecute(DisplayServiceActivity.java:116)
05-12 02:49:44.563: E/AndroidRuntime(19288): at com.example.hstnc_activity.DisplayServiceActivity$Request.onPostExecute(DisplayServiceActivity.java:1)
05-12 02:49:44.563: E/AndroidRuntime(19288): at android.os.AsyncTask.finish(AsyncTask.java:631)
05-12 02:49:44.563: E/AndroidRuntime(19288): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-12 02:49:44.563: E/AndroidRuntime(19288): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-12 02:49:44.563: E/AndroidRuntime(19288): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 02:49:44.563: E/AndroidRuntime(19288): at android.os.Looper.loop(Looper.java:137)
05-12 02:49:44.563: E/AndroidRuntime(19288): at android.app.ActivityThread.main(ActivityThread.java:4918)
05-12 02:49:44.563: E/AndroidRuntime(19288): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 02:49:44.563: E/AndroidRuntime(19288): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 02:49:44.563: E/AndroidRuntime(19288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-12 02:49:44.563: E/AndroidRuntime(19288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-12 02:49:44.563: E/AndroidRuntime(19288): at dalvik.system.NativeStart.main(Native Method)
これは、サーバー経由で取得して処理する必要があるものです
["Categories",[[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]]]