내 응용 프로그램과 몇 가지 문제가 있고 무엇이 잘못되었는지 확실하지 않습니다. 기본적으로 프로그램은 내 안드로이드 장치에로드되며 아무 것도하지 않고 앉아 있습니다. console/logcat에 오류 메시지가 표시되지 않으며 textviews의 텍스트는 다른 사람이 통찰력을 갖고 있는지 궁금해하지 않습니다. 여기 URL에서 AyncTask JSON
11-03 21:55:21.474: I/System.out(16741): execute
11-03 21:55:21.474: I/System.out(16741): started
11-03 21:55:21.524: D/libEGL(16741): loaded /system/lib/egl/libEGL_tegra.so
11-03 21:55:21.544: D/libEGL(16741): loaded /system/lib/egl/libGLESv1_CM_tegra.so
11-03 21:55:21.554: D/libEGL(16741): loaded /system/lib/egl/libGLESv2_tegra.so
11-03 21:55:21.584: D/OpenGLRenderer(16741): Enabling debug mode 0
는 URL
{
"fruit": [
{
"type": "apple",
"color": "green"
},
{
"type": "orange",
"color": "orange"
},
{
"type": "banana",
"color": "yellow"
}
]
}
이 안드로이드 장치에 대한 코드를하려고 내 첫 번째 시간에서 내 JSON 파일입니다 아무것도 발생하지 왜 아무 생각의 브라우저에서로드 한 장치에 URL에 대한 액세스 권한이 있으며로드가 잘되는 것 같습니다.
package com.example.fruitjson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
static String url = "http://192.168.0.14/fruitlist.json";
String TAG_FRUIT = "fruit";
String TAG_TYPE = "type";
String TAG_COLOR = "color";
JSONArray fruit = null;
JSONObject json = null;
InputStream is = null;
JSONObject result = null;
String jsonString = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("execute");
new MyAsyncTask().execute();
System.out.println("started");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class MyAsyncTask extends AsyncTask<String, Void, JSONObject> {
@Override
protected JSONObject doInBackground(String... arg0) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
jsonString = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
result = new JSONObject(jsonString);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return result;
}
protected void onPostExecute(String result) {
// get text views, loop out strings
System.out.println("getting text views");
TextView t1 = (TextView) findViewById(R.id.textView1);
TextView t2 = (TextView) findViewById(R.id.textView3);
System.out.println(t1.getText());
System.out.println(t2.getText());
System.out.println("success");
try {
fruit = json.getJSONArray(TAG_FRUIT);
for (int i = 0; i < fruit.length(); i++) {
JSONObject jObj = fruit.getJSONObject(i);
String type = jObj.getString(TAG_TYPE);
String color = jObj.getString(TAG_COLOR);
t1.setText(type);
t2.setText(color);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
예외가 있는지 확인하거나 catch 블록에서 인쇄하려고 시도했는지 확인하십시오. 문제가 발생하는 곳을 확인하려면 –