AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Problems while connecting andorid app to pandorabots server
 
 

I am trying to connect my android app to the pandorabots server. I am using the following code in android:

package com.example.test2;

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 
Brain {
 
    
public String defaultCustid "0";
    public 
String custid defaultCustid;
    public 
String responseFailed "RESPONSE FAILED";
    public 
String defaultBotId "da43c986ee34039e";
    public 
String defaultHost "www.pandorabots.com";
    
String botResponse;
    
    public 
String askPandorabots(String input{
        
return askPandorabots(inputdefaultHostdefaultBotId);
    
}
    
public String askPandorabots(String inputString hostString botid{
        
//System.out.println("Entering askPandorabots with input="+input+" host ="+host+" botid="+botid);
        
String responseContent pandorabotsRequest(inputhostbotid);
        if (
responseContent == null) return responseFailed;
        else return 
pandorabotsResponse(responseContenthostbotid);
    
}
    
public String responseContent(String urlthrows 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 hostString botidString custidString 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 inputString hostString botid{
        
try {

            String spec 
spec(hostbotidcustidinput);
            
//System.out.println("Spec = "+spec);
            
String responseContent responseContent(spec);
            return 
responseContent;
        
catch (Exception ex{
            ex
.printStackTrace();
            return 
null;
        
}
    }
    
public String pandorabotsResponse (String xmlRpcResponseString hostString botid{
        
        
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 0custid custid.substring(0n2);
                else 
custid defaultCustid;
                
}
            
if (botResponse.endsWith(".")) botResponse botResponse.substring(0botResponse.length()-1);   // snnoying Pandorabots extra "."
        
catch (Exception ex{
            ex
.printStackTrace();
        
}
        
return botResponse;
    
}

and this is my another class which I am using to display the user input and bot response,

package com.example.test2;

//import java.io.IOException;

//import org.apache.http.client.ClientProtocolException;
//import org.apache.http.client.HttpClient;
//import org.apache.http.impl.client.DefaultHttpClient;
//import org.json.JSONException;
//import org.json.JSONObject;



import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
//import android.widget.ImageView;
//import android.widget.RelativeLayout;

public class MainActivity extends Activity {


 String usertext
String response
 
String input=this.usertext;
 
Brain br = new Brain();
 
 
 @
Override
 
protected void onCreate(Bundle savedInstanceState{
  
// TODO Auto-generated method stub
  
super.onCreate(savedInstanceState);
  
setContentView(R.layout.activity_main); 
  final 
LinearLayout ll1 = (LinearLayoutfindViewById(R.id.ll1);
  final 
LinearLayout ll2 = (LinearLayoutfindViewById(R.id.ll2);
  final 
ScrollView scv = (ScrollViewfindViewById(R.id.sv);
  final 
Button btn = (ButtonfindViewById(R.id.button1);
  final 
EditText medit = (EditTextfindViewById(R.id.editText1);
  
  
  
  
  
  
btn.setOnClickListener(new View.OnClickListener() {
  
  
@SuppressLint("NewApi")
  @
Override
  
public void onClick(View v{
  
// TODO Auto-generated method stub
  //ImageView imgu=new ImageView(v.getContext());
  //ImageView imgb=new ImageView(v.getContext());
   
    
TextView tvu=new TextView(v.getContext());
    
TextView tvb=new TextView(v.getContext());
    
    
TextView tvut=new TextView(v.getContext());
    
TextView tvbt=new TextView(v.getContext());
    
    
TextView tvdivider1=new TextView(v.getContext());
    
TextView tvdivider2=new TextView(v.getContext());  
    
    final 
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENTLayoutParams.WRAP_CONTENT);
    
    
//imgu.setLayoutParams(lparams);
    //imgb.setLayoutParams(lparams);
    
    
tvu.setLayoutParams(lparams);
    
tvb.setLayoutParams(lparams);
    
    
tvut.setLayoutParams(lparams);
    
tvbt.setLayoutParams(lparams);
    
    
tvdivider1.setLayoutParams(lparams);
    
tvdivider2.setLayoutParams(lparams);
    
        
    
usertext medit.getText().toString();
    if(
usertext.trim().length() != 0){
     
     ll1
.addView(tvu);
     
ll1.addView(tvb);
     
ll2.addView(tvut);
     
ll2.addView(tvbt);
     
ll1.addView(tvdivider1);
     
ll2.addView(tvdivider2);
     
     
//imgu.setImageResource(R.drawable.user);
     //imgb.setImageResource(R.drawable.user);
     
     
tvu.setText("User");
     
tvb.setText("MAVIS");
     
     
response=br.askPandorabots(input);
     
     
tvbt.setText(" : "response );
     
tvut.setText(" : "usertext);
     
medit.setText(" ");
     
     
tvdivider1.setText("   ");
     
tvdivider2.setText("   --------------------");
    
}
    else{
     
//do nothing
    
}
 }
 }
);
  
  
scv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            
public void onGlobalLayout() {
                scv
.post(new Runnable() {
                    
public void run() {
                        scv
.fullScroll(View.FOCUS_DOWN);
                    
}
                }
);
            
}
        }
);
 
}



 

But i am getting a response failed error. I don’t know what is happeing. Can anyone help me?

 

 
  login or register to react