Answer the question
In order to leave comments, you need to log in
Why can't I send an Http request?
When I try to send a request to the server and get a response, I get the following error. I've been scratching my head for 2 days now. Please help me solve this sudoku. I highlighted in bold the line where I get the error.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pilig.sms3, PID: 9992
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1321)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
at libcore.io.IoBridge.connectErrno(IoBridge.java:150)
at libcore.io.IoBridge.connect(IoBridge.java:133)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:457)
at java.net.Socket.connect(Socket.java:928)
at com.squareup.okhttp.internal.Platform$Android.connectSocket(Platform.java:220)
at com.squareup.okhttp.Connection.connect(Connection.java:148)
at com.squareup.okhttp.OkHttpClient$1.connect(OkHttpClient.java:84)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:321)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
at com.squareup.okhttp.Call.getResponse(Call.java:198)
at com.squareup.okhttp.Call.execute(Call.java:80)
at com.example.pilig.sms3.Requester.request(Requester.java:61)
at com.example.pilig.sms3.Requester.getSMSToSend(Requester.java:80)
at com.example.pilig.sms3.End$2.handleMessage(End.java:60)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5955)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:940)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:801)
I/Process: Sending signal. PID: 9992 SIG: 9
Application terminated.
package com.example.pilig.sms3;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
public class Requester {
String path;
String password;
BufferedReader in;
public Requester getThis(){
return this;
}
private String request(String sql){
final Response[] responce = new Response[1];
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(sql).build();
final Call call = client.newCall(request);
Response response = client.newCall(request).execute();return response.body().string();
} catch (MalformedURLException e) {
return null;
} catch (ProtocolException e) {
return null;
} catch (IOException ex) {
return null;
}
}
public void getConfiguration() throws IOException {
path = in.readLine();
password = in.readLine();
}
public void getSMSToSend() {
String sql = "http://80.252.241.43/telephony/android/get_sms.php?login=" + Account.login + "&password=" + Account.password;
System.out.println(sql);
String text = request(sql);
String phone = text.substring(0, text.indexOf("|"));
text = text.substring(text.indexOf("|")+1);
String sms = text.substring(0, text.indexOf("|"));
String id = text.substring(text.indexOf("|")+1);
SMS message = new SMS(phone, sms, id, getThis());
Account.list.add(message);
}
public void setStatus(String status, String id) {
String sql = "http://80.252.241.43/telephony/android/send_report.php?login=" +
Account.login + "&password=" + Account.password + "&id=" + id + "&status=" + status;
request(sql);
}
}
Answer the question
In order to leave comments, you need to log in
What is there to decide for two days, they would search Google for "NetworkOnMainThreadException" and already write something, it remains only to understand. :)
Here I have written about employees, this is about inefficient people like you)))))
It is necessary in a separate thread - either Thread
, or AsyncTask
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question