无法在android中查看json数据到listview

时间:2013-03-19 07:36:34

标签: android web-services listview

现在我正在尝试在android中使用一个Web服务并尝试将json数据显示到列表视图中。但我无法查看列表视图,同时我没有在我的log-cat上收到任何错误或警告。

请查找我的参考资料

Png.java

public class Png extends Activity{

EditText edt1, edt2;
TextView txtv1;
Button btn;
Bundle data;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_png);

    edt1 = (EditText) findViewById(R.id.editText1);
    edt2 = (EditText) findViewById(R.id.editText2);
    btn = (Button) findViewById(R.id.button1);

    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            getstation(edt1.getText().toString(), edt2.getText().toString());
            Intent intnt = new Intent(v.getContext(), StationsListActivivity.class);
            intnt.putExtras(data);
            startActivity(intnt);
        }
    });
}
public void getstation(String user, String pass) 
{
    System.setProperty("http.keepAlive", "false");
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.dotNet = false;

    String METHOD = "verifyUser";
    String NAMESPACE = "http://service.twinkle.org";
    String SOAP_ACTION = "http://service.twinkle.org/verifyUser";
    String URL = "http://192.168.1.2:8080/WeatherPNG/services/StationDetailsJson?wsdl";

    SoapObject request = new SoapObject(NAMESPACE, METHOD);

    request.addProperty("userName", user.trim());
    request.addProperty("password", pass.trim());
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try 
    {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

        String result_data = result.toString(); 

        data = new Bundle();
        data.putString("transfered_data", result_data);

    //  txtv1 = (TextView)findViewById(R.id.textView1);
    //  txtv1.setText(""+result);

    }catch(Exception e){
        e.printStackTrace();
        }
    }
}

StationsListActivivity.java

public class StationsListActivivity extends Activity {

TextView txt;
String res_data;

Button btn_back;
ListView listView;
ProgressDialog progressDialog;
List<Stations> list;
/*Handler handler=new Handler()
{
    public void handleMessage(android.os.Message msg) 
    {

    }
};*/
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.stations_list_layout);

    //txt = (TextView)findViewById(R.id.textView1);

    Bundle extras=getIntent().getExtras();
    if(extras!=null)
    {
        res_data =extras.getString("transfered_data");
    }
//  txt.setText(res_data);


String jsonString=res_data.toString();

//  System.out.println(jsonString);

final Handler handler =new Handler()
{
    public void handleMessage(android.os.Message msg) 
    {
        if(progressDialog.isShowing())
        {
        progressDialog.cancel();
        if(list.size() > 0)
        {
        listView.setAdapter(new StationAdapter(StationsListActivivity.this, android.R.id.text1,list));  
        }
        else
        {
        Toast.makeText(StationsListActivivity.this, "List is Empty",Toast.LENGTH_LONG).show();  
            }
        }

    System.out.println("Transparent  "+android.R.color.transparent);
    listView = (ListView) findViewById(R.id.listView1);
    btn_back = (Button)findViewById(R.id.btn_info);
    progressDialog = new ProgressDialog(StationsListActivivity.this);
    progressDialog.setTitle("Loading");
    progressDialog.setMessage("Please Wait");
    btn_back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intnt = new Intent(StationsListActivivity.this , Informations_Activity.class);
            startActivity(intnt);           
        }
    });
    Thread thread =new Thread()
    {

        @Override
        public void run() {
            callService();
            sendEmptyMessage(0);
        }
    };
    thread.start();
    progressDialog.show();
}
};
};

public void callService() {
    // TODO Auto-generated method stub
    try {

    JSONArray array = new JSONArray(String.valueOf(res_data).replace("verifyUserResponse{return=", "").replace("; }", ""));
    list = new ArrayList<Stations>();
    for (int i = 0; i < array.length(); i++) 
    {
        System.out.println(array.getJSONObject(i).get("1")+" == "+array.getJSONObject(i).get("0"));;
        Stations stations =new Stations();
        stations.station_code = (String) array.getJSONObject(i).get("0");
        stations.station_name = (String) array.getJSONObject(i).get("1");
        list.add(stations);
        stations = null;
    }

//JSONObject jsonObject = new JSONObject(String.valueOf(so));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

StationAdapter.java

public class StationAdapter extends ArrayAdapter<Stations> {

Context context;
List<Stations> list;

public StationAdapter(Context context, int textViewResourceId,
        List<Stations> objects) {
    super(context, textViewResourceId, objects);
    this.context = context ; 
    this.list = objects ;
    // TODO Auto-generated constructor stub
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.station_child_layout, null);
    TextView name = (TextView) view.findViewById(R.id.textView_list_item);
    ImageView img = (ImageView)view.findViewById(R.id.imageView1);

    Stations stations =list.get(position);
    name.setText(stations.station_name);
    return view;

}

}

Sations.java

package com.example.png_weather;
public class Stations 
{
public String station_name ="";
public String station_code ="";
}

Manifestfile.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.png_weather"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.png_weather.Png"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.example.png_weather.StationsListActivivity"
        android:label="@string/app_name" ></activity>

    <activity
        android:name="com.example.png_weather.Informations_Activity"
        android:label="@string/app_name" ></activity>

</application>

</manifest>

感谢您宝贵的时间!..

0 个答案:

没有答案