0

当我单击按钮时,此消息显示在模拟器显示上

我的 Radio 应用程序出现问题。当我在模拟器中运行应用程序时,我单击按钮,然后应用程序说:

抱歉,应用程序 Radio(进程 org.example.radio)意外停止。请再试一次。

过去还没有这样做,我对从这里去哪里感到困惑。我看了一下 logcat,但我不确定我应该寻找什么以及如何修复它。这是我的代码

- this is my code


package com.example.radio;

    import java.sql.ResultSet;
    import java.sql.SQLException;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

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

        Button b = (Button) findViewById(R.id.button1);
        final TextView Txt = (TextView) findViewById(R.id.textView1);

        b.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                WebService webs = new WebService();
                webs.response();
                ResultSet st = webs.ResultReturn();
                try
                {
                    while(st.next())
                    {
                        Txt.setText(st.getString("uname"));
                    }
                } catch (Exception e) {
                    Txt.setText(e.getMessage());
                }

            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}

- 这是 LogCat 上的错误

07-26 20:14:07.116: I/ActivityManager(61): Start proc com.example.radio for activity com.example.radio/.MainActivity: pid=1117 uid=10034 gids={3003}
07-26 20:14:08.485: I/ActivityManager(61): Displayed com.example.radio/.MainActivity: +3s330ms (total +36s5ms)
07-26 20:14:13.705: D/dalvikvm(697): GC_EXPLICIT freed 99K, 49% free 2980K/5831K, external 5894K/7299K, paused 177ms
07-26 20:14:25.165: D/AndroidRuntime(1117): Shutting down VM
07-26 20:14:25.165: W/dalvikvm(1117): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-26 20:14:25.215: E/AndroidRuntime(1117): FATAL EXCEPTION: main
07-26 20:14:25.215: E/AndroidRuntime(1117): java.lang.ClassCastException: org.json.JSONObject
07-26 20:14:25.215: E/AndroidRuntime(1117):     at com.example.radio.WebService.JsonData(WebService.java:111)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at com.example.radio.WebService.response(WebService.java:54)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at com.example.radio.MainActivity$1.onClick(MainActivity.java:31)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.view.View.performClick(View.java:2485)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.view.View$PerformClick.run(View.java:9080)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.os.Handler.handleCallback(Handler.java:587)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.os.Looper.loop(Looper.java:123)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invoke(Method.java:507)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at dalvik.system.NativeStart.main(Native Method)
07-26 20:14:25.255: W/ActivityManager(61):   Force finishing activity com.example.radio/.MainActivity
07-26 20:14:25.815: W/ActivityManager(61): Activity pause timeout for HistoryRecord{406a26e0 com.example.radio/.MainActivity}
07-26 20:14:29.835: I/InputDispatcher(61): Application is not responding: Window{407a49e0 com.example.radio/com.example.radio.MainActivity paused=false}.  5010.9ms since event, 5009.8ms since wait started
07-26 20:14:29.835: I/WindowManager(61): Input event dispatching timed out sending to com.example.radio/com.example.radio.MainActivity
07-26 20:14:29.835: I/InputDispatcher(61): Dropping event because the pointer is not down.
07-26 20:14:37.755: W/ActivityManager(61): Activity destroy timeout for HistoryRecord{406a26e0 com.example.radio/.MainActivity}

这是 WebService 类

package com.example.radio;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.TextView;

public class WebService {

    private String _result = null;
    private InputStream _is = null;
    private ResultSet st = null;
    private String HttpUrl = "http://bolanhost.com/livestream/WebService.php";



    //constructor
    public WebService() {

    }
    //the year data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    public void response()
    {
        this.httpPost("http://bolanhost.com/livestream/WebService.php");
        this.Bufferreader();
        this.JsonData();
    }


    private void httpPost(String _HttpUrl)
    {
        nameValuePairs.add(new BasicNameValuePair("uname","Hello"));
        //httpPost
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(_HttpUrl);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            _is = entity.getContent();
        }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }
    }


    private void Bufferreader()
    {

        //convert response to string
        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();

            _result=sb.toString();
        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }
    }

    private void JsonData()
    {
        //parse json data
        try
        {
            JSONArray jArray = new JSONArray(_result);
            for(int i=0;i<jArray.length();i++){
               st = (ResultSet) jArray.getJSONObject(i);

        }


        }
        catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
        }

    }


    public ResultSet ResultReturn()
    {
        return st;
    }


}
4

3 回答 3

3

使用JSONObject,Dont use ResultSet.

因为JSONArray have JSONObject's Array。而且你正在尝试使用 ResultSet。所以两个类都不一样,所以 ClassCastException。

 try{

   JSONArray jArray = new JSONArray(_result);

   for(int i=0;i<jArray.length();i++){

          JSONObject objJson = jArray.getJSONObject(i);
          String strName = objJson.getString("uname"); 

          Log.i("Name is "+strName); 

          }

        }
于 2012-07-27T11:53:18.203 回答
1

查看堆栈跟踪 - 问题在第 111 行。可能是你施放的地方

st = (ResultSet) jArray.getJSONObject(i);

getJSONObject(i) 返回 JSONObject 而不是 ResultSet。检查 JSON 文档。

另一件事是您不应该在主线程上使用 Web 服务。尝试使用 AsyncTask 进行网络访问。

于 2012-07-27T12:02:51.050 回答
0

请检查您的网络服务响应,因为您的错误显示 ClassCastException。

于 2012-07-27T11:46:38.317 回答