0
我覺得我提供的代碼是我最好的選擇。發送HTTP發佈請求崩潰Android程序
主要活動
public class MainActivity extends ActionBarActivity {
Button submitbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submitbtn = (Button) findViewById(R.id.button);
submitbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PostDataAsyncTask runner = new PostDataAsyncTask();
runner.execute();
}
});
}
其它方法和類中的主要活動
PostDataAsyncTask(未使用的被覆蓋的省略的方法)
public class PostDataAsyncTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
postData();
return null;
}
}
P在Mainifest文件ostData()方法
private void postData(){
try{
String postReceiverUrl = "staffappfeedback.comule.com/insert-dp.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(postReceiverUrl);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("name", "Mike"));
nameValuePairs.add(new BasicNameValuePair("summary", "My suggestion"));
nameValuePairs.add(new BasicNameValuePair("description", "This is what I think you should do"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我已經啓用了互聯網接入。
許多類/對象上都有刪除線(即 - 已棄用)。
爲什麼會這樣?
這是我的程序崩潰的原因嗎?
我的android設備有調試問題,所以我無法輕鬆找到問題,因此在Stack Overflow上的帖子,但不要誤解我的意思,我花了很多時間試圖找到問題。
任何想法?
使用AppCompat活動而不是ActionBarActivity。這不是解決方案。只是一個建議。由於谷歌終於決定使用AppCompatActivity並且棄用ActionBarActivity。 –
這是否適用於所有版本的android? –
請發佈崩潰日誌 –