无法使用android解析json数据

时间:2015-04-03 10:33:02

标签: android mysql xml json

您好我正在尝试解析MySQL的json数据并在eclipse中将其显示在xml上,但我无法解析它。我在警察注册项目上工作,我有一个用于在图像按钮上显示城市罪犯的表单,所以当点击图像按钮时,我想要在新的xml上显示有关罪犯的信息。必须通过JSON从MySQL解析信息,但我无法做到这一点。以下是我正在处理的代码

xml文件          

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="64dp"
        android:text="Name:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="24dp"
        android:text="CrimeType:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="26dp"
        android:text="Age:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="19dp"
        android:text="Sex:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="22dp"
        android:text="Info:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView6"
        android:layout_below="@+id/textView6"
        android:layout_marginTop="82dp"
        android:text="Height:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/tvname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView3"
        android:layout_marginLeft="40dp"
        android:layout_toRightOf="@+id/textView3"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvcrimetype"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView4"
        android:layout_alignLeft="@+id/tvname"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView5"
        android:layout_alignLeft="@+id/tvcrimetype"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvsex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView6"
        android:layout_alignLeft="@+id/tvage"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView7"
        android:layout_alignLeft="@+id/tvsex"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvheight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvinfo"
        android:layout_alignTop="@+id/textView7"
        android:text="TextView" />

    <Button
        android:id="@+id/Bwanteddesing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="14dp"
        android:text="Button" />

</RelativeLayout>

wantedcriminalsdetails.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class Wantedgetdata extends Activity {
         TextView criminalname;
         TextView criminalage;
         TextView criminalcrimetype;
         private ProgressDialog pDialog;
         String criminal_id;
         JSONParser jsonParser = new JSONParser();
         private static final String url_criminal_detials = "http://divaexample.honor.es/wanted_criminals.php";

         private static final String TAG_SUCCESS = "success";
         private static final String TAG_POSTS = "posts";
         private static final String TAG_CRIMINAL_ID = "CRIMINAL_ID";
         private static final String TAG_NAME = "SUSPECT_NAME";
         private static final String TAG_AGE_RANGE = "AGE_RANGE";



         @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.wanted_design_details);
             Intent i = getIntent();
             criminal_id = i.getStringExtra(TAG_CRIMINAL_ID);
             new GetProductDetails().execute();
        }

         class GetProductDetails extends AsyncTask<String, String, String> {

             /**
              * Before starting background thread Show Progress Dialog
              * */
             @Override
             protected void onPreExecute() {
                 super.onPreExecute();
                 pDialog = new ProgressDialog(Wantedgetdata.this);
                 pDialog.setMessage("Loading criminal details. Please wait...");
                 pDialog.setIndeterminate(false);
                 pDialog.setCancelable(true);
                 pDialog.show();
             }

         protected String doInBackground(String... params) {

             // updating UI from Background Thread
             runOnUiThread(new Runnable() {
                 public void run() {
                     // Check for success tag
                     int success;
                     try {
                         // Building Parameters
                         List<NameValuePair> params = new ArrayList<NameValuePair>();
                         params.add(new BasicNameValuePair("criminal_id", criminal_id));

                         // getting product details by making HTTP request
                         // Note that product details url will use GET request
                         JSONObject json = jsonParser.makeHttpRequest(
                                 url_criminal_detials, "GET", params);

                         // check your log for json response
                         Log.d("Single criminal Details", json.toString());

                         // json success tag
                         success = json.getInt(TAG_SUCCESS);
                         if (success == 1) {
                             // successfully received product details
                             JSONArray criminalObj = json
                                     .getJSONArray(TAG_POSTS); // JSON Array

                             // get first product object from JSON Array
                             JSONObject criminal = criminalObj.getJSONObject(0);

                             // product with this pid found
                             // Edit Text
                             criminalname = (TextView) findViewById(R.id.tvname);
                             criminalage = (TextView) findViewById(R.id.tvage);


                             // display product data in EditText
                             criminalname.setText(criminal.getString(TAG_NAME));
                             criminalage.setText(criminal.getString(TAG_AGE_RANGE));


                         }else{
                             // product with pid not found
                         }
                     } catch (JSONException e) {
                         e.printStackTrace();
                     }
                 }
             });

             return null;
         }

         protected void onPostExecute(String file_url) {
             // dismiss the dialog once got all details
             pDialog.dismiss();
         }


         }
}

0 个答案:

没有答案
相关问题