简体   繁体   中英

HTTP post Doesn't Work in my android app

String HttpPost(String aURL) throws Exception
    {
        URL url = new URL(aURL);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String response="";
        String inputLine;

        while ((inputLine = in.readLine()) != null)
            response+=inputLine;    

      //  Log.i(TAG, response);
        if(in!=null)
         in.close();
        return response;
   }


public void userpost(View v){

        txtUsername = (EditText) findViewById(R.id.txtUserName);
        txtUserphone = (EditText) findViewById(R.id.txtUserphone);
        txtUserphone = (EditText) findViewById(R.id.txtUsermail);

        String Username = txtUsername.getText().toString();
        String Userphone = txtUserphone.getText().toString();
        String Usermail = txtUserphone.getText().toString();

        //action=staffadd&&sname=test1&&mobile=78545656566&&email=abr@abr.com&&imei=78945641235645
        String conString = "";
        conString = "action=staffadd";
        conString = url + "?" + conString +"&&sname=" +Username +"&&mobile=" +Userphone +"&&email=" + Usermail +"&&imei="+iIMEI;

        try {
            String response =HttpPost(conString);

            Log.i("REQUEST", conString);
            Log.i("Response", response);

            if (response.startsWith("Y")){

                return;
            }

            else if(response.startsWith("N"))
                return;

        } catch (Exception e) {

        }   

    }

If I click Submit button I got

02-13 00:13:02.103: I/Choreographer(553): Skipped 53 frames! The application may be doing too much work on its main thread.

I develop this app in android 4.4

The problem is you are using web service in the UI or main thread so you need to use AsyncTask for the web services. See this link . Which give the idea how to use AsyncTask. This is most common error

That link mention you everything. I just add code only here.

Step 1: First you need to call the Asynctask like this

new DownloadFilesTask().execute(url1, url2, url3);

Step 2: Implement your Async class

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

Note: There is three methods onPreExecute() , doInBackground() , and onPostExecute() . Do your download works under doInBackground() method.

It is just sample code. Try your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM