我正在处理申请,我需要从网站上复制一份联系表格。 http://www.telekino.com.ar/info/contacto。我理解获取数据的jsoup原则,但我对发送数据的概念并不擅长。 并且提交按钮没有网页或邮件地址,以便我在哪里发送数据。
这是我的代码
public class Contactenos extends Fragment {
public Contactenos(){}
private DrawerLayout mDrawerLayout;
public static String namet;
public static String localidadt;
public static String telefonot;
public static String emailt;
public static String consultat;
public static String maildestt;
ProgressDialog mProgressDialog;
String url = "http://www.telekino.com.ar/info/contacto/#contactenos";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.contacto, container, false);
EditText name=(EditText)rootView.findViewById(R.id.nombre);
EditText localidad=(EditText)rootView.findViewById(R.id.localidad);
EditText telefono=(EditText)rootView.findViewById(R.id.telefono);
EditText email=(EditText)rootView.findViewById(R.id.email);
EditText consulta=(EditText)rootView.findViewById(R.id.consulta);
RadioGroup rg = (RadioGroup) rootView.findViewById(R.id.maildest);
RadioButton rb = (RadioButton) rg.findViewById(rg.getCheckedRadioButtonId());
maildestt= String.valueOf(rb);
namet = name.getText().toString();
localidadt = localidad.getText().toString();
telefonot = telefono.getText().toString();
emailt = email.getText().toString();
consultat=consulta.getText().toString();
Button sendbut= (Button)rootView.findViewById(R.id.sendbutton);
sendbut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Title().execute();
}
});
return rootView;
}
// Title AsyncTask
private class Title extends AsyncTask<Void, Void, Void> {
String sorteofech;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setTitle("TELEKINO");
mProgressDialog.setMessage("enviando...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Connect to the server and do a post
try {
// Connect to the web site
Document doc = Jsoup.connect(url)
.data("nombre", namet)
.data("localidad", localidadt)
.data("telefono", telefonot)
.data("email", emailt)
.data("dpto", maildestt)
.data("consulta", consultat)
.post();
} catch (IOException e) {
e.printStackTrace();
}
mProgressDialog.dismiss();
}
}
}
这种方式崩溃,我不确定是否能正常工作。
这个是Logcat
07-15 12:37:35.899 1329-1329/info.blacktrail.telekino E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.blacktrail.telekino, PID: 1329
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
at com.android.okio.Okio$2.read(Okio.java:113)
at com.android.okio.RealBufferedSource.exhausted(RealBufferedSource.java:58)
at com.android.okhttp.internal.http.HttpConnection.isReadable(HttpConnection.java:144)
at com.android.okhttp.Connection.isReadable(Connection.java:259)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:110)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:449)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:434)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:181)
at org.jsoup.helper.HttpConnection.post(HttpConnection.java:176)
at info.blacktrail.telekino.Contactenos$Title.onPostExecute(Contactenos.java:107)
at info.blacktrail.telekino.Contactenos$Title.onPostExecute(Contactenos.java:79)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
如何修复它或在android上回复此联系表单的最佳方法是什么?
答案 0 :(得分:1)
在doInBackground中调用connect方法。然后它将在后台线程中运行网络请求。
OnPostExecute将在主线程上运行,而不是在后台运行。要执行后台任务,请使用doInBackGround。
如果要从http请求的结果更新UI,请使用doInBackGround。