3

在这里发布之前,我已经检查了许多替代方案。
我不明白为什么我会收到这个错误,比如..请UnKnownHost Exception有人指导我找到解决方案。

MyCalling 类

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)
4

2 回答 2

2

SOAP API转向使用XMLRPC API解决了我的问题。 下面是我使用 XMLRPC的代码

你必须去DownLoad图书馆XMLRPC

单击此处下载适用于 Android 的 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;
       }
   }
}
于 2012-07-23T10:43:21.837 回答
0

您应该检查 URL 是否正确。

于 2012-07-18T07:46:02.090 回答