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) {
}
}
如果我点击提交按钮我
02-13 00:13:02.103:I / Choreographer(553):跳过53帧!该 应用程序可能在其主线程上做了太多工作。
我在android 4.4中开发这个应用程序
答案 0 :(得分:1)
问题是您在UI或主线程中使用Web服务,因此您需要将AsyncTask用于Web服务。 见link。这给出了如何使用AsyncTask的想法。 这是最常见的错误
这个链接提到你的一切。我只在这里添加代码。
第1步: 首先,您需要像这样调用Asynctask
new DownloadFilesTask().execute(url1, url2, url3);
第2步: 实现您的Async类
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");
}
}
注意:有三种方法onPreExecute()
,doInBackground()
和onPostExecute()
。您的下载是否适用于doInBackground()
方法。
这只是示例代码。试试你自己的。