我需要在几个区域使用loopj的SyncHttpClient。当我使用AsyncHttpClient时,请求成功返回。当我按照接受的答案How to use loopJ SyncHttpClient for synchronous calls?中所示使用SyncHttpClient时,我在onFailure
中遇到了一个断点。 statusCode 为0, errorResponse 为空, throwable 为 java.io.IOException:未处理的异常:null
这是相关代码。我再次使用Async时效果很好:
buttonTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// AsyncHttpClient httpClient = new AsyncHttpClient();
SyncHttpClient httpClient = new SyncHttpClient();
httpClient.get("http://10.0.1.6:3000/home/test_endpoint", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
String stringResponse = response.toString();
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
String error = errorResponse.toString();
}
});
String temp = "got here";
}
});
我正在使用compile 'com.loopj.android:android-async-http:1.4.9'
答案 0 :(得分:2)
当我点击这个时我做了一些调试,我发现loopj正在吃以下异常:
<?
require ('dbquery.php');
?>
<script>
function matchWord(e){
if(document.getElementById('userN').value.trim() === '<? '".$GET['username'].'"; ?>){
document.getElementById('Upass').disabled = true;
alert('User already exist choose another');
}
else{
document.getElementById('Upass').disabled = false;
}
}
document.getElementById('userN').addEventListener("keyup", function(evt) {
matchWord(evt);
}, false);
</script>
<tr><th>Username</th><td><input type = 'text' name='usname' id="userN' value="30" /></td><br>
<tr><th>Password</th><td><input type = 'password' name='pasew' id='Upass' value="20"/></td></tr><br>
<input type ="submit" id="submit" value="submit" />
我无法提供详细的堆栈跟踪,因为这个异常似乎没有正确记录(由于某种原因,logcat根本没有提取它)
但是,它位于:
android.os.NetworkOnMainThreadException
我一直在运行我的代码:
AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:203)
正如您所看到的,我碰巧在Handler.post()方法中运行了SyncHttpClient,但显然实现不算作(非主线程上的网络)
修复它的关键是使用&#34;新线程(...)&#34;那个mikeorr85的代码包括在内。像这样......
Handler handler = new Handler();
Runnable r = new Runnable(){
public void run(){
SyncHttpClient client ....
client.get(.....); // hit error here
}
};
答案 1 :(得分:1)
您仍然必须在主线程上运行它。这个代码对我有用,并允许我在请求完成后点击设置为“String temp ='got here'”的断点:
buttonTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
// AsyncHttpClient httpClient = new AsyncHttpClient();
SyncHttpClient httpClient = new SyncHttpClient();
httpClient.get("http://10.0.1.6:3000/home/test_endpoint", null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
String stringResponse = response.toString();
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
String error = errorResponse.toString();
}
});
String temp = "got here";
}
}).start();
}
github页面上提供了更多代码:https://github.com/loopj/android-async-http/blob/master/sample/src/main/java/com/loopj/android/http/sample/SynchronousClientSample.java
答案 2 :(得分:0)
我使用AsyncHttpClient,它对我很有用。