При попытке отправить запрос серверу и получить ответ получаю следующую ошибку. Уже ломаю голову 2 дня. Помогите решить это судоку. Жирным начертанием я выделил строку, где ошибку получаю.
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);
}
}