httpclientに問題があります。私はアプリをデバッグし、それは実行機能を呼び出すときにクラッシュします。ここでは、コードです:Android:AsyncTaskでhttpclient.executeを実行するとアプリケーションがクラッシュする
public class MyAsyncTask extends AsyncTask<String, String, String> {
GridView mGridView;
Activity mContex;
public MyAsyncTask(Activity contex) {
this.mContex = contex;
}
@Override
protected String doInBackground(String... params) {
//fetch data
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("localhost:3000/api/send");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", params[1]));
nameValuePairs.add(new BasicNameValuePair("name", params[0]));
nameValuePairs.add(new BasicNameValuePair("gender", "male"));
nameValuePairs.add(new BasicNameValuePair("result", "lazy bee"));
nameValuePairs.add(new BasicNameValuePair("age", "28"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return "success";
}
}
私はフラグメントから、ここMyAsyncTaskを呼び出し、アクティビティを渡す:
public class SendResultPageFragment extends Fragment implements View.OnClickListener {
EditText name, email;
MyAsyncTask task;
public static SendResultPageFragment newInstance() {
return new SendResultPageFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_screen_slide_send_result, container, false);
rootView.findViewById(R.id.result_next_button).setOnClickListener(this);
name = (EditText)rootView.findViewById(R.id.name_txt);
email = (EditText)rootView.findViewById(R.id.email_txt);
return rootView;
}
@Override
public void onClick(View view) {
this.task = new MyAsyncTask(getActivity());
this.task.execute(this.name.getText().toString(), this.email.getText().toString());
return ;
}
}
誰もが解決策を知っていますか?ありがとう
いくつかのエラーメッセージをご提供ください。 – Remario
'新しいHttpPost(" localhost:3000/api/send ");'。あなたはプロトコルを忘れてしまった。したがって、新しいHttpPost( "http:// localhost:3000/api/send"); 'にする必要があります。しかし、あなたのアプリが動くのと同じデバイス上にサーバがなければ、localhostはやりません。あなたの設定を教えてください。あなたは何と何をつなぎたいのですか? – greenapps
'実行したときにクラッシュします。それであなたが捉えなかった例外は? – greenapps