无法调用Web服务 [英] Fail to call web service
问题描述
LOG CAT
LOG CAT
05-21 10:56:27.399 21874-22114/com.example.isandeep.trydemo I/global﹕ In close() at SocketHttpClientConnection<br />
05-21 10:56:27.409 21874-22114/com.example.isandeep.trydemo I/global﹕ call createSocket() return a new socket.<br />
05-21 10:56:27.439 21874-21874/com.example.isandeep.trydemo I/Adreno200-EGLSUB﹕ <ConfigWindowMatch:1991>: Format RGBA_8888.<br />
05-21 10:56:30.131 21874-21874/com.example.isandeep.trydemo V/AsyncHttpResponseHandler﹕ Progress 86 from 196 (44%)<br />
05-21 10:56:30.131 21874-21874/com.example.isandeep.trydemo D/Res:﹕ [ 05-21 10:56:30.161 22093:0x5660 I/AccountTypeManager ]<br />
Loaded meta-data for 11 account types, 10 accounts in 371ms(wall) 90ms(cpu)<br />
05-21 10:56:30.191 21874-21874/com.example.isandeep.trydemo W/System.err﹕ org.json.JSONException: End of input at character 1 of<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at org.json.JSONTokener.syntaxError(JSONTokener.java:450)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at org.json.JSONTokener.nextValue(JSONTokener.java:97)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:154)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:171)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at com.example.isandeep.trydemo.MainActivity$2.onSuccess(MainActivity.java:98)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at com.example.isandeep.trydemo.MainActivity$2.onSuccess(MainActivity.java:87)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:311)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:138)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at android.os.Looper.loop(Looper.java:156)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4987)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)<br />
05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo D/OpenGLRenderer﹕ Flushing caches (mode 0)<br />
05-21 10:56:30.211 21874-21874/com.example.isandeep.trydemo D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x4e7c8000 size:3338240 offset:3215360<br />
05-21 10:56:30.211 21874-21874/com.example.isandeep.trydemo D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x4eaf7000 size:3579904 offset:3457024<br />
05-21 10:56:30.211 21874-21874/com.example.isandeep.trydemo D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x4ef51000 size:3702784 offset:3579904<br />
05-21 10:56:33.705 21874-21874/com.example.isandeep.trydemo D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x4e59e000 size:3579904 offset:3457024<br />
05-21 11:00:20.516 21874-21874/com.example.isandeep.trydemo W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection<br />
05-21 11:00:20.526 21874-21874/com.example.isandeep.trydemo W/IInputConnectionWrapper﹕ InputConnection = com.android.internal.widget.EditableInputConnection@40e1b8d0, active client = false<br />
Android代码
导入android.app.ProgressDialog;
导入android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem ;
导入android.view.View;
import andro id.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj。 android.http.RequestParams;
公共类MainActivity扩展ActionBarActivity {
EditText et1,et2,et3,et4,et5,et6,et7,et8;
Button btn;
ProgressDialog prgDialog;
@覆盖
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); < br $>
//实例化进度对话框对象
prgDialog = new ProgressDialog(this);
//设置进度对话框文本
prgDialog.setMessage(Please wait ...);
//将Cancelable设置为False
prgDialog .setCancelable(false);
et1 =(EditText)findViewById(R.id.Demo1);
et2 =(EditText)findViewById(R.id.Demo2);
et3 =(EditText)findViewById(R.id.Demo3);
et4 =(EditText )findViewById(R.id.Demo4);
et5 =(EditText)findViewById(R.id.Demo5);
et6 =(EditText)findViewById(R.id .Demo6);
et7 =(EditText)findViewById(R.id.Demo7);
et8 =(EditText)findViewById(R.id.Demo8);
btn =(按钮)findViewById(R.id.btnDemo);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
RequestParams para ms = new RequestParams();
params.put(Name,et1.getText()。toString());
params.put(Addre, et2.getText()。toString());
params.put(Gen,et3.getText()。toString());
params.put( Phone,et4.getText()。toString());
params.put(mobile,et5.getText()。toString());
params.put(Occu,et6.getText()。toString());
params.put(UserNm,et7.getText()。toString());
params.put(Pwd,et8.getText()。toString());
prgDialog.show();
invokeWS(params);
}
});
}
public void invokeWS(RequestParams params){
//显示进度对话框
prgDialog.show();
//制作RESTful使用AsyncHttpClient obj进行webservice调用ect
AsyncHttpClient client = new AsyncHttpClient();
//
client.get( http://MyDomain.com/MyService.asmx/ReceiveInsertUsser\",params,new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int i,Header [] header,byte [] bytes){
onSuccess();
}
public void onSuccess (字符串响应){
Log.d(Res:,响应);
/ /隐藏进度对话框
prgDialog.hide();
尝试{
// JSON对象
JSONObject obj = new JSONObject(response);
//当JSON响应的状态布尔值赋值为true
if(obj.getString(status)!= null) {
//使用Toast显示已成功注册的消息
Toast.makeText(getApplicationContext(),你已成功注册!,Toast.LENGTH_LONG)。show();
}
else {//否则显示错误信息
Log.d(错误:,obj.getString(error_msg));
Toast.makeText(getApplicationContext(),obj.getString(error_msg),Toast.LENGTH_LONG)。show();
}
} catch(JSONException e) {
// TODO自动生成的捕获块
Toast.makeText(getApplicationContext(),错误发现[服务器的JSON响应可能无效]!,Toast.LENGTH_LONG ).show();
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode,Header [] headers,byte [] bytes,Throwable throwable){
//隐藏进度对话框
prgDialog.hide();
if(statusCode == 404){
Toast.makeText(getApplicationContext (),找不到请求的资源,Toast.LENGTH_LONG)。show();
Log.d(状态代码:+ statusCode,找不到请求的资源);
}
else if(statusCode == 500){
Log.d(Status Code:+ statusCode ,服务器端出错了);
Toast.makeText(getApplicationContext(),服务器端出错,Toast.LENGTH_LONG)。show();
}
else {
Log.d(状态代码:+ statusCode,发生意外错误! [最常见的错误:设备可能未连接到Internet或远程服务器未启动并运行]);
Toast.makeText(getApplicationContext(),发生意外错误![最常见的错误:设备可能未连接到Internet或远程服务器未启动并运行],Toast.LENGTH_LONG)。show();
}
}
});
}
}
Android code
import android.app.ProgressDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class MainActivity extends ActionBarActivity {
EditText et1,et2,et3,et4,et5,et6,et7,et8;
Button btn;
ProgressDialog prgDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate Progress Dialog object
prgDialog= new ProgressDialog(this);
// Set Progress Dialog Text
prgDialog.setMessage("Please wait...");
// Set Cancelable as False
prgDialog.setCancelable(false);
et1 = (EditText)findViewById(R.id.Demo1);
et2 = (EditText)findViewById(R.id.Demo2);
et3 = (EditText)findViewById(R.id.Demo3);
et4 = (EditText)findViewById(R.id.Demo4);
et5 = (EditText)findViewById(R.id.Demo5);
et6 = (EditText)findViewById(R.id.Demo6);
et7 = (EditText)findViewById(R.id.Demo7);
et8 = (EditText)findViewById(R.id.Demo8);
btn = (Button)findViewById(R.id.btnDemo);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RequestParams params = new RequestParams();
params.put("Name",et1.getText().toString());
params.put("Addre",et2.getText().toString());
params.put("Gen",et3.getText().toString());
params.put("Phone",et4.getText().toString());
params.put("mobile",et5.getText().toString());
params.put("Occu",et6.getText().toString());
params.put("UserNm",et7.getText().toString());
params.put("Pwd",et8.getText().toString());
prgDialog.show();
invokeWS(params);
}
});
}
public void invokeWS(RequestParams params){
// Show Progress Dialog
prgDialog.show();
// Make RESTful webservice call using AsyncHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
//""
client.get("http://MyDomain.com/MyService.asmx/ReceiveInsertUsser",params ,new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, Header[] headers, byte[] bytes) {
onSuccess(" ");
}
public void onSuccess(String response) {
Log.d("Res: ",response);
// Hide Progress Dialog
prgDialog.hide();
try {
// JSON Object
JSONObject obj = new JSONObject(response);
// When the JSON response has status boolean value assigned with true
if(obj.getString("status")!=null){
// Display successfully registered message using Toast
Toast.makeText(getApplicationContext(), "You are successfully registered!", Toast.LENGTH_LONG).show();
}
else{// Else display error message
Log.d("Error: ", obj.getString("error_msg"));
Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] bytes, Throwable throwable) {
// Hide Progress Dialog
prgDialog.hide();
if(statusCode == 404){
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
Log.d("Status Code: " + statusCode,"Requested resource not found");
}
else if(statusCode == 500){
Log.d("Status Code: " + statusCode,"Something went wrong at server end");
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
else{
Log.d("Status Code: " + statusCode,"Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]");
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
}
}
推荐答案
2.onSuccess(MainActivity.java:98)< br />
05-21 10:56:30.201 21874-21874 / com.example.isandeep.trydemo W / System.err :at com.example.isandeep.trydemo.MainActivity
2.onSuccess(MainActivity.java:98)<br /> 05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at com.example.isandeep.trydemo.MainActivity
2.onSuccess(MainActivity.java:87)< br />
05-21 10:56:30.201 21874 -21874 / com.example.isandeep.trydemo W / System.err:at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:311)< br />
05-21 10:56 :30.201 21874-21874 / com.e xample.isandeep.trydemo W / System.err:at com.loopj.android.http.AsyncHttpResponseHandler
2.onSuccess(MainActivity.java:87)<br /> 05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:311)<br /> 05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at com.loopj.android.http.AsyncHttpResponseHandler
ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:138)< br />
05-21 10:56:30.201 21874-21874 / com.example.isandeep.trydemo W / System.err:at android.os.Handler.dispatchMessage(Handler.java:99)< br />
05-21 10:56:30.201 21874-21874 / com.example.isandeep.trydemo W / System.err:at android.os.Looper.loop(Looper.java:156)< br />
05 -21 10:56:30.201 21874-21874 / com.example.isandeep.trydemo W / System.err:at android.app.ActivityThread.main(ActivityThread.java:4987)< br />
05- 21 10:56:30.201 21874-21874 / com.example.isandeep.trydemo W / System.err:at java.lang.reflect.Method.invokeNative(Native Method)< br />
05-21 10 :56:30.201 21874-21874 / com.example.isandeep.trydemo W / System.err:at java.lang.reflect.Method.invoke(Method.java:511)< br />
05-21 10:56:30.201 21874-21874 / com.example.isandeep.trydemo W / System.err:a t com.android.internal.os.ZygoteInit
ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:138)<br /> 05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)<br /> 05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at android.os.Looper.loop(Looper.java:156)<br /> 05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4987)<br /> 05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)<br /> 05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)<br /> 05-21 10:56:30.201 21874-21874/com.example.isandeep.trydemo W/System.err﹕ at com.android.internal.os.ZygoteInit
这篇关于无法调用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!