gpt4 book ai didi

Android/OkHttp - client.newCall(request).execute() 总是返回异常

转载 作者:行者123 更新时间:2023-12-04 23:48:04 26 4
gpt4 key购买 nike

我必须制作一个android应用程序项目。起初,我尝试使用 HttpURLConnection 但它不起作用。所以在和 friend 讨论之后,我尝试使用OkHttp。我一直对“responses = client.newCall(request).execute();”有一个异常(exception)。经过长时间的搜索,我只是尝试了这段代码,这是“https://github.com/square/okhttp/wiki/Recipes”的教程
而且.....它也不起作用!
我的问题是,到底发生了什么?我目前正在 Android Studio 1.5.1 下开发 4.0.3 应用程序。我还添加了以下两个依赖项:

// DEPENDENCIES
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

// Class Http
public static String run(String url) throws IOException {
try {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormEncodingBuilder()
.add("login", "Mr. X")
.add("password", "********")
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Response responses = null;

try {
Log.d("DEBUGG ", "----------------------------------");
responses = client.newCall(request).execute();
Log.d("DEBUGG ", "----------------------------------");
return (responses.body().string());
} catch (IOException e) {
e.printStackTrace();
}
String jsonData = responses.body().string();
JSONObject Jobject = new JSONObject(jsonData);
JSONArray Jarray = Jobject.getJSONArray("employees");

for (int i = 0; i < Jarray.length(); i++) {
JSONObject object = Jarray.getJSONObject(i);
}
} catch (JSONException e) {

}
return null;
}

// MainActivity
private TextView textView;
private Button button;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (TextView)findViewById(R.id.textViewJSon);
button = (Button)findViewById(R.id.Hit);

textView.setText("Hello !");

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
textView.setText(Http.run("https://epitech-api.herokuapp.com/login"));
} catch (Exception e) {
e.printStackTrace();
}
}
});

}

最佳答案

{答案}
我终于尝试使用像 Selvin 所说的多线程编程并且效果很好所以解决方案是打开另一个线程

public static int responseCode = 0;
public static String responseString = "";

public static Thread login = new Thread(new Runnable() {
private OkHttpClient client = new OkHttpClient();
private String url = "https://epitech-api.herokuapp.com/login";
private User user = User.getUser();

public void run() {
try {
// Build the request
RequestBody formBody = new FormEncodingBuilder()
.add("login", user._login)
.add("password", user._password)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Response responses = null;

// Reset the response code
responseCode = 0;

// Make the request
responses = client.newCall(request).execute();

if ((responseCode = responses.code()) == 200) {
// Get response
String jsonData = responses.body().string();

// Transform reponse to JSon Object
JSONObject json = new JSONObject(jsonData);

// Use the JSon Object
user._token = json.getString("token");
}

} catch (IOException e) {
responseString = e.toString();
} catch (JSONException e) {
responseString = e.toString();
}
}
});

关于Android/OkHttp - client.newCall(request).execute() 总是返回异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34902265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com