Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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 获取错误java.io.FileNotFoundException:_Android_Http_Android Asynctask_Inputstream_Filenotfoundexception - Fatal编程技术网

Android 获取错误java.io.FileNotFoundException:

Android 获取错误java.io.FileNotFoundException:,android,http,android-asynctask,inputstream,filenotfoundexception,Android,Http,Android Asynctask,Inputstream,Filenotfoundexception,我正在尝试读取电话联系人并将该列表发送到我的服务器,但在某些电话中获取FileNotFound错误,但程序在某些电话中工作正常。下面是我的课- public class UpdateNetwork extends Activity { private String server, server_response; private String updatenetwork = "/update-network.php";

我正在尝试读取电话联系人并将该列表发送到我的服务器,但在某些电话中获取
FileNotFound
错误,但程序在某些电话中工作正常。下面是我的课-

public class UpdateNetwork extends Activity {

    private String              server, server_response;
    private String              updatenetwork   = "/update-network.php";
    private String              data_to_send;
    public JSONArray            jacontacts;
    public JSONObject           jcontacts;
    public ArrayList<String>    contacts;
    private ProgressDialog      pd;
    private Long                user_id;
    private MainActivity        main;
    JSONObject                  jo              = null;
    Button                      update;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.updatenetwork);
        server = getString(R.string.server_add_new);

        contacts = new ArrayList<String>();
        main = new MainActivity();
        update = (Button) findViewById(R.id.update);
        update.setVisibility(View.INVISIBLE);
        user_id = main.getDefaultsint("user_id", UpdateNetwork.this);
        Updatenetwork un = new Updatenetwork(true);
        un.execute();

        update.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                URL url = null;
                try {
                    url = new URL(server + updatenetwork);
                    data_to_send = "contacts=" + jcontacts + "&";
                    data_to_send += "user_id=" + user_id;
                    Updatenetwork un = new Updatenetwork(url, data_to_send);
                    un.execute();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

    public class Updatenetwork extends AsyncTask<String, Integer, JSONObject> {
        private URL url;
        Boolean     un_send = false;
        String      data_to_send;

        public Updatenetwork(Boolean b) {
            this.un_send = b;
        }

        public Updatenetwork(URL url, String data_to_send2) {
            this.url = url;
            this.data_to_send = data_to_send2;
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            super.onPostExecute(result);
            if (un_send) {
                update.setVisibility(View.VISIBLE);
            } else {
                finish();
            }

            pd.dismiss();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = ProgressDialog.show(UpdateNetwork.this, "Please wait...", "Sending...");

        }

        @Override
        protected JSONObject doInBackground(String... params) {
            Log.e("value of un_send", "********"+un_send+jcontacts);
            if(un_send){
                ContentResolver cr = getContentResolver();
                Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                        null, null, null, null);

                if (cur.getCount() > 0) {
                    while (cur.moveToNext()) {
                        String id   = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                        // String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                            /*   System.out.println("name : " + name );
                             */
                            // get <span id="IL_AD4" class="IL_AD">the phone number</span>
                            Cursor pCur =     cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                    new String[]{id}, null);
                            while (pCur.moveToNext()) {
                                String phone = pCur.getString(
                                        pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                contacts.add(phone);
                                jacontacts = new JSONArray();
                                /* System.out.println("phone" + phone);*/
                            }
                            for(int i=0; i<contacts.size(); i++){
                                jacontacts.put(contacts.get(i)); 

                            }
                            jcontacts = new JSONObject();
                            try{
                                jcontacts.put("contacts", jacontacts);
                            }catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            /* Log.e("value inside", "json obj"+jcontacts);*/
                            pCur.close();
                            jo = jcontacts;
                        }
                    }
                }
            } else {

                BufferedReader reader=null; 
                try
                { 
                    Log.e("inside try block ", "get text 11111111" + data_to_send);

                    URLConnection conn  =  url.openConnection(); 
                    Log.e("inside try block ", "get text 2222222" + data_to_send);
                    conn.setDoOutput(true);                   
                    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
                    wr.write(data_to_send);
                    wr.flush();     

                    // Get the server response 
                    reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line = null;

                    // Read Server Response
                    while((line = reader.readLine()) != null)
                    {
                        // Append server response in string    
                        sb.append(line + "\n");
                        Log.e("inside ", "while loop");
                    }

                    server_response = sb.toString();
                    Log.e("server_response ", "server_response" +server_response);
                }
                catch(FileNotFoundException(String detailMessage))
                {
                    Log.e("MyTag ", "Failure to get json data in --1--", ex);
                    ex.printStackTrace();
                }
                finally
                {
                    try
                    {               
                        reader.close();
                    }

                    catch(Exception ex) {
                        Log.e("MyTag", "Failure to get json data in ", ex);
                    }
                }

                try {
                    jo = new JSONObject(server_response);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return jo;
        }
    }
}

如果手机可以访问服务器,请尝试URL的Uri.parse()。wifi/3g等是否已打开?是的…手机中已启用wifi演示您的手机使用手机上的浏览器查看您是否可以直接访问该文件@jenuine他说有些手机正在工作,所以我怀疑是否存在许可(清单)问题
12-18 16:44:21.922: E/MyTag(32005): Failure to get json data in --1--
12-18 16:44:21.922: E/MyTag(32005): java.io.FileNotFoundException: http:// server address/update-network.php
12-18 16:44:21.922: E/MyTag(32005):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:194)