尝试使用AsyncTask将JSON数据从localhost解析为片段android时出现问题。
这是我的代码FindPeopleFragment.java
public class FindPeopleFragment extends Fragment {
private ProgressDialog pDialog;
String nim;
JSONArray str_json = null;
String surl = "http://10.0.2.2/siakad/jadwal.php?nim=125410295&&jam=10:00 - 12:30";
TextView isinya;
public FindPeopleFragment(Bundle b){
nim = b.getString("par_kode");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (container == null)
{
return null;
}
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
isinya=(TextView)rootView.findViewById(R.id.isi);
new AmbilData().execute();
return rootView;
}
class AmbilData extends AsyncTask<String, Void, JSONObject> {
String matkul;
String ruang;
String dosen;
String sks;
String jamkul;
String kelas;
String hari;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Sabar gan, masih ngambil data neh...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected JSONObject doInBackground(String... result) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.AmbilJson(surl);
return json;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
pDialog.dismiss();
try {
str_json = result.getJSONArray("jadwal");
for(int i = 0; i < str_json.length(); i++){
JSONObject ar = str_json.getJSONObject(i);
matkul= ar.getString("matkul");
sks = ar.getString("sks");
hari = ar.getString("hari");
jamkul = ar.getString("jam");
ruang = ar.getString("ruang");
kelas = ar.getString("kelas");
dosen = ar.getString("dosen");
isinya.setText("Matakuliah : "+matkul+
"\nSks : "+sks+
"\nHari : "+hari+
"\nJam : "+jamkul+
"\nRuang : "+ruang+
"\nKelas : "+kelas+
"\nDosen : "+dosen);
} catch (JSONException e) {
e.printStackTrace();
Log.e("hasil", "Failed data was:\n" + result);
}
}
}
}
和我的JSONParser.java
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject AmbilJson(String url) {
// 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();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
我的LogCat中有一些错误:
01-31 04:25:46.900: E/AndroidRuntime(2985): FATAL EXCEPTION: AsyncTask #4
01-31 04:25:46.900: E/AndroidRuntime(2985): java.lang.RuntimeException: An error occured while executing doInBackground()
01-31 04:25:46.900: E/AndroidRuntime(2985): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-31 04:25:46.900: E/AndroidRuntime(2985): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-31 04:25:46.900: E/AndroidRuntime(2985): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-31 04:25:46.900: E/AndroidRuntime(2985): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-31 04:25:46.900: E/AndroidRuntime(2985): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-31 04:25:46.900: E/AndroidRuntime(2985): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-31 04:25:46.900: E/AndroidRuntime(2985): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-31 04:25:46.900: E/AndroidRuntime(2985): at java.lang.Thread.run(Thread.java:841)
01-31 04:25:46.900: E/AndroidRuntime(2985): Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 58: http://10.0.2.2/siakad/jadwal.php?nim=125410295&&jam=10:00 - 12:30
01-31 04:25:46.900: E/AndroidRuntime(2985): at java.net.URI.create(URI.java:727)
01-31 04:25:46.900: E/AndroidRuntime(2985): at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
01-31 04:25:46.900: E/AndroidRuntime(2985): at sob.akademik.JSONParser.AmbilJson(JSONParser.java:35)
01-31 04:25:46.900: E/AndroidRuntime(2985): at sob.akademik.FindPeopleFragment$AmbilData.doInBackground(FindPeopleFragment.java:164)
01-31 04:25:46.900: E/AndroidRuntime(2985): at sob.akademik.FindPeopleFragment$AmbilData.doInBackground(FindPeopleFragment.java:1)
01-31 04:25:46.900: E/AndroidRuntime(2985): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-31 04:25:46.900: E/AndroidRuntime(2985): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-31 04:25:46.900: E/AndroidRuntime(2985): ... 4 more
请帮我解决这个问题。谢谢。
答案 0 :(得分:0)
如果仔细观察堆栈跟踪,将会显示所有内容。您提供的网址无效。
考虑使用android.net.Uri.Builder
构建有效的URI。
例如:
new Uri.Builder()
.scheme("http")
.authority("10.0.2.2")
.appendPath("siakad")
.appendPath("jadwal.php")
.appendQueryParameter("nim", "125410295")
.appendQueryParameter("jam", "10:00 - 12:30")
.build();