java – 在Fragments中使用JSON时,应用程序在Android开发中没有
发布时间:2020-12-15 04:16:31 所属栏目:Java 来源:网络整理
导读:我在 android中使用导航抽屉.我通过 JSON从URL获取数据.当我在加载片段时运行应用程序时会出现错误 “Unfortunately the App has not responded ” I’m getting the JSON Data from a PHP file and it is taken from the INTERNET for now it is in the loc
我在
android中使用导航抽屉.我通过
JSON从URL获取数据.当我在加载片段时运行应用程序时会出现错误
我的片段类是: import java.util.ArrayList; import java.util.HashMap; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class QMFragment extends Fragment { public QMFragment(){} // url to make request private static String url = "http://localhost/app/qm_data.php"; // JSON Node names private static final String TAG_QM = "qm"; private static final String TAG_ID = "id"; private static final String TAG_NAME = "name"; private static final String TAG_URL = "url"; // Hashmap for ListView ArrayList<HashMap<String,String>> contactList = new ArrayList<HashMap<String,String>>(); // JSONArray JSONArray qmArray = null; // Creating JSON Parser instance JSONParser jParser = new JSONParser(); // getting JSON string from URL JSONObject json = jParser.getJSONFromUrl(url); @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_qm,container,false); getData(); return rootView; } public void getData(){ try { // Getting Array qmArray = json.getJSONArray(TAG_QM); // looping through All Data for(int i = 0; i < qmArray.length(); i++){ JSONObject c = qmArray.getJSONObject(i); // Storing each json item in variable String id = c.getString(TAG_ID); String name = c.getString(TAG_NAME); String url = c.getString(TAG_URL); // creating new HashMap HashMap<String,String> map = new HashMap<String,String>(); // adding each child node to HashMap key => value map.put(TAG_ID,id); map.put(TAG_NAME,name); map.put(TAG_URL,url); // adding HashList to ArrayList contactList.add(map); } } catch (JSONException e) { e.printStackTrace(); } } } 而我的JSONParser.java是: 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.JSONException; import org.json.JSONObject; import android.util.Log; public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(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; } } 我的XML是: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/bg" > <TextView android:id="@+id/txtLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="40dp" android:layout_marginTop="5dp" android:text="@string/head" android:textColor="@color/text_color" android:textSize="25sp" /> <TextView android:layout_width="fill_parent" android:layout_height="1px" android:layout_alignParentLeft="true" android:layout_below="@+id/txtLabel" android:layout_marginTop="5dp" android:background="#CCCCCC" /> <ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/txtLabel" android:layout_marginTop="10dp" > <RelativeLayout android:id="@+id/navigate_RLayout" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/txtqmhead" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/qm_heading" android:textSize="25sp" android:textColor="@color/text_color" /> <TextView android:id="@+id/txtqmsub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/qm_chn" android:textSize="18sp" android:textColor="@color/text_color" android:layout_below="@+id/txtqmhead" android:layout_marginLeft="28dp" /> <TableLayout android:id="@+id/tbl" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/txtqmsub" android:layout_marginTop="20dp" > </TableLayout> </RelativeLayout> </ScrollView> </RelativeLayout> 和PHP文件是: <?php header('Content-type: application/json'); $server = "localhost"; $username = "root"; $password = ""; $db = "test"; $con = mysql_connect("localhost",$username,$password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($db,$con); $result = mysql_query("SELECT * FROM example ORDER BY date DESC"); while($row = mysql_fetch_assoc($result)) { $output['qm'][]=$row; } print(json_encode($output)); mysql_close($con); ?> LogCat: 12-24 10:19:54.322: E/AndroidRuntime(1700): FATAL EXCEPTION: main 12-24 10:19:54.322: E/AndroidRuntime(1700): android.os.NetworkOnMainThreadException 12-24 10:19:54.322: E/AndroidRuntime(1700): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084) 12-24 10:19:54.322: E/AndroidRuntime(1700): at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 12-24 10:19:54.322: E/AndroidRuntime(1700): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 12-24 10:19:54.322: E/AndroidRuntime(1700): at java.net.InetAddress.getAllByName(InetAddress.java:220) 12-24 10:19:54.322: E/AndroidRuntime(1700): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 12-24 10:19:54.322: E/AndroidRuntime(1700): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 12-24 10:19:54.322: E/AndroidRuntime(1700): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 12-24 10:19:54.322: E/AndroidRuntime(1700): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 12-24 10:19:54.322: E/AndroidRuntime(1700): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 12-24 10:19:54.322: E/AndroidRuntime(1700): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 12-24 10:19:54.322: E/AndroidRuntime(1700): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 12-24 10:19:54.322: E/AndroidRuntime(1700): at com.qrodsintegrated.JSONParser.getJSONFromUrl(JSONParser.java:38) 12-24 10:19:54.322: E/AndroidRuntime(1700): at com.qrodsintegrated.QMFragment.<init>(QMFragment.java:41) 12-24 10:19:54.322: E/AndroidRuntime(1700): at com.qrodsintegrated.MainActivity.displayView(MainActivity.java:175) 12-24 10:19:54.322: E/AndroidRuntime(1700): at com.qrodsintegrated.MainActivity.access$0(MainActivity.java:164) 12-24 10:19:54.322: E/AndroidRuntime(1700): at com.qrodsintegrated.MainActivity$SlideMenuClickListener.onItemClick(MainActivity.java:126) 12-24 10:19:54.322: E/AndroidRuntime(1700): at android.widget.AdapterView.performItemClick(AdapterView.java:292) 12-24 10:19:54.322: E/AndroidRuntime(1700): at android.widget.AbsListView.performItemClick(AbsListView.java:1058) 12-24 10:19:54.322: E/AndroidRuntime(1700): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514) 12-24 10:19:54.322: E/AndroidRuntime(1700): at android.widget.AbsListView$1.run(AbsListView.java:3168) 12-24 10:19:54.322: E/AndroidRuntime(1700): at android.os.Handler.handleCallback(Handler.java:605) 12-24 10:19:54.322: E/AndroidRuntime(1700): at android.os.Handler.dispatchMessage(Handler.java:92) 12-24 10:19:54.322: E/AndroidRuntime(1700): at android.os.Looper.loop(Looper.java:137) 12-24 10:19:54.322: E/AndroidRuntime(1700): at android.app.ActivityThread.main(ActivityThread.java:4340) 12-24 10:19:54.322: E/AndroidRuntime(1700): at java.lang.reflect.Method.invokeNative(Native Method) 12-24 10:19:54.322: E/AndroidRuntime(1700): at java.lang.reflect.Method.invoke(Method.java:511) 12-24 10:19:54.322: E/AndroidRuntime(1700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 12-24 10:19:54.322: E/AndroidRuntime(1700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-24 10:19:54.322: E/AndroidRuntime(1700): at dalvik.system.NativeStart.main(Native Method) 解决方法
你有
JSONObject json = jParser.getJSONFromUrl(url); 在JSONParser中 HttpResponse httpResponse = httpClient.execute(httpPost); 在ui线程上运行NetworkRelated操作. 使用AsyncTask或Thread http://developer.android.com/reference/android/os/AsyncTask.html new TheTask().execute(params); 然后 class TheTask extends AsyncTask<Void,Void,Void> { protected void doInbackground(Void... params) { // your http request return null; } } 你可以使asynctask成为片段的内部类,并在onPostExecute中更新ui 编辑: @Override public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_qm,false); new TheTask().execute(url); return rootView; } 然后 class TheTask extends AsyncTask<String,String,String> { @override protected String doInbackground(String... params) { String _response =null; try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(params[0]); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); _response=EntityUtils.toString(httpEntity); }catch(Exception e) { e.printStacktrace(); } return _response; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); Log.i(".......",result); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |