2014-01-13 28 views
1

我試着在Android中創建一個應用程序chatbot。我使用Pandorabots作爲Chatbot服務器。 Device Android與服務器之間的連接。我使用pandorabot XML-RPC API,並使用android-xmlrpc中的xml-rpc庫。所以這個我的代碼:XML-RPC錯誤,org.xmlpull.v1.XmlPullParserException

public class MainActivity extends Activity { 
private EditText editOne; 
private TextView textOne; 
private Button ButtonOne; 
private XMLRPCClient server; 
private URI uri; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (android.os.Build.VERSION.SDK_INT > 9) { 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
    } 

    uri = URI.create("http://www.pandorabots.com/pandora/talk-xml?botid=e80e92407e341007"); 
    server = new XMLRPCClient(uri); 
    editOne = (EditText) findViewById(R.id.editText1); 
    textOne = (TextView) findViewById(R.id.textView1); 
    ButtonOne = (Button) findViewById(R.id.button1); 

    textSatu.setText(getDataMethod("hi")); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 


private String getDataMethod(String num) { 
    String text = ""; 
    try { 
     Log.w("Running server.call", "prosess"); 
     Object[] data = (Object[]) server.call("input", num); 
     Log.w("server.call Run", "finish"); 
     Log.w("Run HashMap", "prosess"); 
     for(Object o: data) { 
      HashMap map = (HashMap) o; 
      Log.w("HashMap Berjalan", "Error"); 
      text = text + "'that' => " + map.get("that") + "\n\n"; 
     } 
    } catch (XMLRPCException e) { 
     Log.w("XMLRPC Test", "Error", e); 
     text = "XMLRPC error"; 
    }  
    return text; 
} 


} 

但我得到錯誤。這就是說:org.xmlpull.v1.XmlPullParserException:expected:START_TAG {null} methodResponse(position:START_TAG @ 1:45 in [email protected]

任何人都可以幫助我嗎?請。

+0

後的XML,請。編輯:沒關係,我看到它。 –

+0

對不起。我沒有明白你的意思。你的意思是在這裏http://www.pandorabots.com/pandora/talk-xml?botid=e80e92407e341007&input=hi? –

+0

我幾乎認爲你錯誤地使用了'XMLRPCClient',但我無法分辨,因爲我找不到任何文檔。 –

回答

1

這裏是一個不需要XMLRPCClient的解決方案。重要的是在與bot首次交互時捕獲客戶ID,然後將custid的值與每個後續事務一起發回。機器人所使用的客戶ID記住與會話線程相關聯的局部變量,如姓名,年齡,性別,主題等

import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URI; 
import java.net.URLEncoder; 
public class PandorabotsTalkAPI { 
    public String defaultCustid = "0"; 
    public String custid = defaultCustid; 
    public String responseFailed = "RESPONSE FAILED"; 
    public String defaultBotId = "f5d922d97e345aa1"; 
    public String defaultHost = "www.pandorabots.com"; 
    public String askPandorabots(String input) { 
     return askPandorabots(input, defaultHost, defaultBotId); 
    } 
    public String askPandorabots(String input, String host, String botid) { 
     //System.out.println("Entering askPandorabots with input="+input+" host ="+host+" botid="+botid); 
     String responseContent = pandorabotsRequest(input, host, botid); 
     if (responseContent == null) return responseFailed; 
     else return pandorabotsResponse(responseContent, host, botid); 
    } 
    public String responseContent(String url) throws Exception { 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(); 
     request.setURI(new URI(url)); 
     InputStream is = client.execute(request).getEntity().getContent(); 
     BufferedReader inb = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(""); 
     String line; 
     String NL = System.getProperty("line.separator"); 
     while ((line = inb.readLine()) != null) { 
      sb.append(line).append(NL); 
     } 
     inb.close(); 
     return sb.toString(); 
    } 


    public String spec(String host, String botid, String custid, String input) { 
     //System.out.println("--> custid = "+custid); 
     String spec = ""; 
     try { 
      if (custid.equals("0"))  // get custid on first transaction with Pandorabots 
       spec = String.format("%s?botid=%s&input=%s", 
         "http://" + host + "/pandora/talk-xml", 
         botid, 
         URLEncoder.encode(input, "UTF-8")); 
      else spec =     // re-use custid on each subsequent interaction 
        String.format("%s?botid=%s&custid=%s&input=%s", 
          "http://" + host + "/pandora/talk-xml", 
          botid, 
          custid, 
          URLEncoder.encode(input, "UTF-8")); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     //System.out.println(spec); 
     return spec; 
    } 

    public String pandorabotsRequest(String input, String host, String botid) { 
     try { 

      String spec = spec(host, botid, custid, input); 
      //System.out.println("Spec = "+spec); 
      String responseContent = responseContent(spec); 
      return responseContent; 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
      return null; 
     } 
    } 
    public String pandorabotsResponse (String xmlRpcResponse, String host, String botid) { 
     String botResponse = responseFailed; 
     try { 
      int n1 = xmlRpcResponse.indexOf("<that>"); 
      int n2 = xmlRpcResponse.indexOf("</that>"); 

      if (n2 > n1) 
       botResponse = xmlRpcResponse.substring(n1+"<that>".length(), n2); 
      n1 = xmlRpcResponse.indexOf("custid="); 
      if (n1 > 0) { 
       custid = xmlRpcResponse.substring(n1+"custid=\"".length(), xmlRpcResponse.length()); 
       n2 = custid.indexOf("\""); 
       if (n2 > 0) custid = custid.substring(0, n2); 
       else custid = defaultCustid; 
       } 
      if (botResponse.endsWith(".")) botResponse = botResponse.substring(0, botResponse.length()-1); // snnoying Pandorabots extra "." 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return botResponse; 
    } 

} 
+0

感謝@Arch Botmaster,幾周前pandorabot的Harry也給出了同樣的解決方案,這是工作。但我忘了編輯這個線程。非常感謝你 –

相關問題