创建Android应用程序时出错

时间:2016-12-20 13:16:08

标签: java android classcastexception

private class WeatherTask extends AsyncTask<String , Void, Weather>{
    @Override
    protected Weather doInBackground(String... params) {
        String data=((new WeatherHttpClient()).getWeatherData(params[0]));

        weather = JSONWeatherParser.getWeather(data);

        Log.v("Data:",weather.currentCondition.getDescreption());
        return weather;
    }

我是android开发的新手,有人可以帮我解决运行应用程序时发生的异常错误

FATAL EXCEPTION: AsyncTask #1
Process: com.example.elangocandy.weatherapp, PID: 2764
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassCastException: com.android.okhttp.internal.huc

2 个答案:

答案 0 :(得分:0)

Caused by: java.lang.ClassCastException: com.android.okhttp.internal.huc

当您尝试将对象转换为实际不存在的类型时,抛出ClassCastException。我认为WeatherHttpClient是您编写的类。因此,如果您在此类中进行类转换,则应在转换前执行instance of检查。

如果您可以发布WeatherHttpClient课程的正文,我可以提供更多详细信息。

您是否正在使用排球库进行网络通话?

答案 1 :(得分:0)

public class WeatherHttpClient {
public String getWeatherData(String place){
    HttpsURLConnection connection=null;
    InputStream inputStream=null;

    try {
        connection = (HttpsURLConnection)(new URL(Utils.BASE_URL + place)).openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.connect();

        //Read the Response
        StringBuffer stringBuffer=new StringBuffer();
        inputStream=connection.getInputStream();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
        String line=null;

        while ((line=bufferedReader.readLine()) != null){
            stringBuffer.append(line + "\r\n");
        }
        inputStream.close();
        connection.disconnect();

         return stringBuffer.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
      return null;
}

}