Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android IntentService类出错_Android_Service_Httpclient - Fatal编程技术网

Android IntentService类出错

Android IntentService类出错,android,service,httpclient,Android,Service,Httpclient,我有一个IntentService类,它解析来自服务器的JSON文件,并将属性保存到SQLite数据库中。 但每当我启动它,应用程序就会崩溃,stacktrace上就会显示一个错误。 有人能帮我解决这个错误吗? 谢谢 这是我的班级: public class DBSync extends IntentService { //holds new Location[] temporarily, well be released after storing data to DB pu

我有一个IntentService类,它解析来自服务器的JSON文件,并将属性保存到SQLite数据库中。 但每当我启动它,应用程序就会崩溃,stacktrace上就会显示一个错误。 有人能帮我解决这个错误吗? 谢谢

这是我的班级:

public class DBSync extends IntentService {

    //holds new Location[] temporarily, well be released after storing data to DB
    public static VideoLocation[] videoLocations = null;

    private static VideoLocationReceiver receiver = null;

    public DBSync() {
        super("DBSync");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        VideoLocationList locations = null;
        videoLocations = null;

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(
                "http://historyvision.solutions.smfhq.com/api/locations.json");
        try {
            HttpResponse response = httpClient.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                InputStream instream = response.getEntity().getContent();
                BufferedReader r = new BufferedReader(new InputStreamReader(
                        instream, "UTF-8"), 8000);
                StringBuilder total = new StringBuilder();
                String line;
                while ((line = r.readLine()) != null) {
                    total.append(line);
                }
                instream.close();

                //parse to gson here
                String JSON_Result = total.toString();

                JSON_Result = "{ locations:"+JSON_Result+"}";

                Log.d("DBSync", "JSON_Result:"+JSON_Result);
                Gson gson = new Gson();
                locations = (VideoLocationList)gson.fromJson(JSON_Result, VideoLocationList.class);

                VideoLocationItem[] vidLocItems = locations.getVideoLocations();
                int numLocations = vidLocItems.length;

                videoLocations = new VideoLocation[numLocations];
                for(int i=0; i<vidLocItems.length; i++ ){
                    videoLocations[i] = vidLocItems[i].location;
                }
                if (receiver!=null) {
                    receiver.receivedVideoLocationData(videoLocations);
                }
//              Log.d("DBSync", "resulting json: "+gson.toJson(locations, VideoLocationList.class));
            }
        } catch (ClientProtocolException e) {
            // TODO HANDLE THIS EXCEPTION, E.G. CHECK NETWORK/INFORM USER ETC.
            receiver.receivedVideoLocationData(null);
            e.printStackTrace();
        } catch (IOException e) {
            // TODO HANDLE THIS EXCEPTION, E.G. CHECK NETWORK/INFORM USER ETC.
            e.printStackTrace();
        }

        //only after sucessfully retrieving the remote json we open and update the db
        if (videoLocations != null) {

            JsonDB dbhelper = WWHApplication.getInstance().getJsonDBInstance();
            SQLiteDatabase db = dbhelper.getWritableDatabase();

            //begin transaction for insert
            db.beginTransaction();
            dbhelper.InsertLocationArray(db,videoLocations);
            db.setTransactionSuccessful();//end transaction
            db.endTransaction();
        }

        //fetch db content just for debugging

        JsonDB dbhelper = WWHApplication.getInstance().getJsonDBInstance();
        SQLiteDatabase db = dbhelper.getWritableDatabase();
        VideoLocation[] dbLocations = dbhelper.getVideoLocations(db);
        Gson gs = new Gson();
        for(VideoLocation vl : dbLocations){
            Log.d("DBSync", gs.toJson(vl));
        }
    }

    public static VideoLocation[] getVideoLocations(){
        return videoLocations;
    }


    public static void setVideoLocationReceiver(VideoLocationReceiver vr){
        receiver = vr;
    }

    public interface VideoLocationReceiver {

        public void receivedVideoLocationData(final VideoLocation[] vidLocs);
    }
}

stacktrace显示以下行的错误:for(VideoLocation vl:dbLocations){

由于未知主机异常而导致。您可以通过两种方式修复它:

  • 重新启动模拟器或设备以刷新其DNS缓存
  • 在Try/catch块中为
    UnknownHostException
    添加另一个异常捕获程序。我建议您最好添加一个常规
    异常
    捕获
  • 例如:

    ...
    ...
    catch (UnknownHostExceptione) {
          Log.e("test", "unknown host connection error");
    }
    catch (Exception) {
          Log.e("test", "an error occurred: " + e.toString());
    }
    

    检查您的internet连接、url、mainfest.xml(您从那里获得internet的权限)。错误表明您在调用web服务时遇到问题,而不是存储在sqlite中。此处有问题

    http://historyvision.solutions.smfhq.com/api/locations.json
    

    确保在manifest.xml中声明网络权限

    <uses-permission android:name="android.permission.INTERNET"/>
    
    
    
    <uses-permission android:name="android.permission.INTERNET"/>