Erstellen Sie die JSONParser-Klasse, die den Json-Parsing-Code enthält.
erwähnen Sie in Ihrer MainActivity.java
JSONParser jsonparser = new JSONParser(); JSONObject jsonObject = jsonparser.getJSONFromURL (URL);
Jetzt Erstellen Sie separate JSONParser Klasse
class JSONParser
{
public static JSONObject jsonObject=null;
public static String json=null;
InputStream is=null;
public JSONObject getJSONFromURL(String url)
{
try
{
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost(url);
HttpResponse response=client.execute(post);
HttpEntity entity=response.getEntity();
is=entity.getContent();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuilder sb=-new StringBuilder();
String line=null;
while((br.readLine())!=null)
{
sb.append(line+"\n");
}
json=sb.toString();
is.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
jsonObject=new JSONObject(json.subString(json.indexOf("{"),json.lastinddexOf("}")+1));
}
catch(Exception e)
{
e.printStackTrace();
}
return jsonObject;
}
Jetzt in MainActivity.java
try
{
JSONParser jsonparser=new JSONParser();
JSONObject jsonObject=jsonparser.getJSONFromURL(URL);// URL is a String which contains url
Log.d("Response:",jsonObject.toString());
JSONArray jsonarray=new JSONArray(jsonObject.getString("YourFirstJSONArrayName"));//YourJSONArray contains the response array
for(int i=0;i<jsonarray.length();i++)
{
JSONObject c=jsonarray.getJSONObject(i);
// now get data from c object
}
// Now getting data from Second Array
JSONArray jsona=new JSONArray(jsonObject.getString("YourSecondJSONArrayName"));
for(int j=0;j<jsona.length();j++)
{
JSONObject c=jsona.getJSONObject(j);
// now get data from json data from second array
}
}
catch(Exception e)
{
e.printStackTrace();
}
versuchen, das '‘ ‚' 'mit“ „', zu ersetzen, und wenn es nicht funktioniert, versuchen Sie, mit '" '' –
Funktioniert nicht. Wenn ich meine API ändere und mein Array nenne und dort "myArray" eintippe, funktioniert es ... ohne einen Namen - es tut nicht: S – TheUnreal
Beginnt Ihre Antwort mit diesem Array? –