在这里发帖之前我已经检查了很多替代方案..
我不明白为什么我会收到此错误,例如
UnKnownHost Exception
..请有人指导我解决方案。
我的呼叫类
public class AndroidLoginExampleActivity extends Activity
{
private static final String SOAP_ACTION ="urn:Mage_Api_Model_Server_HandlerAction";
private static final String NAMESPACE = "urn:Magento";
private static final String Method_Name="login";
private static final String URL ="http://cws24/machinetest/api/?wsdl";
TextView tv;
Context mContext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.tv_status);
SoapObject request = new SoapObject(NAMESPACE,Method_Name);
request.addProperty("username", "android");
request.addProperty("apiKey", "android");
SoapSerializationEnvelope envelopes = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelopes.dotNet = false;
envelopes.setOutputSoapObject(request);
mContext=AndroidLoginExampleActivity.this;
try
{
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.debug =true;
androidHttpTransport.call(SOAP_ACTION, envelopes);//Getting the Exception here
SoapPrimitive resultString=(SoapPrimitive)envelopes.getResponse();
tv.setText("Status "+resultString);
new AlertDialog.Builder(mContext).setMessage(""+resultString).show();
}
catch (Exception e) {e.printStackTrace();
new AlertDialog.Builder(mContext).setMessage(""+e.toString()).show();
}
}
}
日志输出::
07-17 02:20:43.376: WARN/System.err(25212): java.net.UnknownHostException: Host is unresolved: cws24:80
07-17 02:20:43.416: WARN/System.err(25212): at java.net.Socket.connect(Socket.java:1037)
07-17 02:20:43.466: WARN/System.err(25212): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
07-17 02:20:43.486: WARN/System.err(25212): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:145)
07-17 02:20:43.496: WARN/System.err(25212): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:67)
07-17 02:20:43.496: WARN/System.err(25212): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:821)
07-17 02:20:43.496: WARN/System.err(25212): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:807)
07-17 02:20:43.496: WARN/System.err(25212): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
07-17 02:20:43.506: WARN/System.err(25212): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
07-17 02:20:43.506: WARN/System.err(25212): at com.magento.example.AndroidLoginExampleActivity.onCreate(AndroidLoginExampleActivity.java:44)
07-17 02:20:43.506: WARN/System.err(25212): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-17 02:20:43.506: WARN/System.err(25212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-17 02:20:43.516: WARN/System.err(25212): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-17 02:20:43.516: WARN/System.err(25212): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-17 02:20:43.516: WARN/System.err(25212): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-17 02:20:43.546: WARN/System.err(25212): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 02:20:43.546: WARN/System.err(25212): at android.os.Looper.loop(Looper.java:123)
07-17 02:20:43.556: WARN/System.err(25212): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-17 02:20:43.556: WARN/System.err(25212): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 02:20:43.567: WARN/System.err(25212): at java.lang.reflect.Method.invoke(Method.java:521)
07-17 02:20:43.567: WARN/System.err(25212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-17 02:20:43.576: WARN/System.err(25212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-17 02:20:43.576: WARN/System.err(25212): at dalvik.system.NativeStart.main(Native Method)
从SOAP API转向使用XMLRPC API已经解决了我的问题。
下面是我使用的代码XMLRPC
您必须
DownLoad
XMLRPC
图书馆才能做到这一点。
package org.xmlrpc;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Test extends Activity {
String sessionId ="";
ProgressDialog pd;
Context mContext;
EditText username,apikey;
Button login;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext=Test.this;
username=(EditText)findViewById(R.id.username);
apikey=(EditText)findViewById(R.id.apikey);
login=(Button)findViewById(R.id.login);
}
public void checkUser(View v)
{
if(username.getText().toString().equals("") || apikey.getText().toString().equals(""))
{
if(username.getText().toString().equals(""))
{
username.requestFocus();
}
else
{
apikey.requestFocus();
}
Toast.makeText(mContext, "Please fill this empty field", Toast.LENGTH_LONG).show();
}
else
{
new DownloadWebPageTask().execute();
}
}
private class DownloadWebPageTask extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute()
{
pd=ProgressDialog.show(mContext, "", "Please Wait...\nLoading data...", false);
}
@Override
protected void onPostExecute(Void v)
{
if(pd.isShowing())
{
pd.dismiss();
}
if(sessionId.equals(""))
{
Toast.makeText(mContext, "in Correct Details..Please try again",
Toast.LENGTH_LONG).show();
}
else
{
Utils.sessionId=sessionId;
startActivity(new Intent(mContext,Dashboard.class));
finish();
}
}
@Override
protected Void doInBackground(Void... arg0)
{
XMLRPCClient client = new XMLRPCClient( "http://192.168.1.237/machinetest/index.php/api/xmlrpc");
try
{
sessionId = (String)client.call( "login", username.getText().toString(),
apikey.getText().toString());
Log.d( "MY_XMLRPC_SUCCESS_SESSION_ID", sessionId);
}
catch (XMLRPCException e)
{
Log.d( "APP_INFO ", "Exception: " + e.getMessage());
Log.e( "APP_INFO ", "Exception: " + e.toString());
}
return null;
}
}
}
请检查网址是否正确。