Monday, May 28, 2012

[android-developers] Webservice in ANDROID, KSOAP and PHP

Hi ;
I am developing an application leads to purchase bar code, but I have a problem in the webservice which is to retrieve information about a product Scane. so my problem is:
when I execute the activity SearchActivity.java and folds the search field I get an error, here is my java class, error, and my server php wsdl:


Activity :SearchActivity:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SearchActivity extends Activity {
TextView nom;
TextView prix;
TextView description;
 private static final String SOAP_ACTION = "urn:WSproduits/getProducts";
 private static final String METHOD_NAME = "getProducts";
 private static final String NAMESPACE = "urn:WSproduits";
 private static final String URL = "http://10.0.2.2:80/WebserviceProduit/WSproduits.php";
 
 
 
@Override
    public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.search_actvity);   
      nom = (TextView)findViewById(R.id.nom);
         prix = (TextView)findViewById(R.id.prix);
         description = (TextView)findViewById(R.id.descriprion);
      Log.i("TAG", "je suis là x");
    
    }
 public SoapObject soap (String property, String value) throws IOException, XmlPullParserException
{
// Création de la requête SOAP
SoapObject request = new SoapObject (NAMESPACE, METHOD_NAME);
 Log.i("TAG", "je suis là p1");
//Ajout de propriété: addProperty(nom de variable, valeur) -> Le nom de la variable vient du fichier WSDL
if (property != null)
request.addProperty(property, value);
 Log.i("TAG", "je suis là p2");
//Toutes les données demandées sont mises dans une enveloppe.
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (
SoapEnvelope.VER11);
 Log.i("TAG", "je suis là p3");
//Préparation de la requête
envelope.setOutputSoapObject (request);
Log.i("TAG", "je suis là p4");
HttpTransportSE androidHttpTransport = new HttpTransportSE (URL);
//Ceci est optionnel, on l'utilise pour savoir si nous voulons ou non utiliser 
//un paquet "sniffer" pour vérifier le message original (androidHttpTransport.requestDump)
Log.i("TAG", "je suis là pp5");
androidHttpTransport.debug = true; 
//Envoi de la requête
androidHttpTransport.call (SOAP_ACTION, envelope);
//Obtention du résultat
 
SoapObject soapResult = (SoapObject) envelope.getResponse();
Log.i("TAG", soapResult.toString());
return soapResult;
}
 
    public void doSearch(View s)
    {
     EditText uname = (EditText)findViewById(R.id.codeSearch);
     String ean = uname.getText().toString();
      try 
      {
      SoapObject soapo=this.soap("ean", ean);
      Log.i("SOAP Resultat : ", " "+ soapo.toString());
         
             // Parse les données JSON
     
      try{
      JSONArray jArray = new JSONArray(soapo.toString());
      for(int i=0;i<jArray.length();i++){
      JSONObject json_data = jArray.getJSONObject(i);
      // Affichage ID_Produite et nom dans le LogCat
      Log.i("log_tag","ID_Produit : "+json_data.getString("ID_Produit")+
      " Nom : "+json_data.getString("Nom")
      );
      nom.setText(json_data.getString("Nom"));
      prix.setText(json_data.getString("Prix"));
      description.setText(json_data.getString("Description"));
      // Résultats de la requête
      //returnString += "\n\t" + jArray.getJSONObject(i); 
      }
      }catch(JSONException e){
      Log.e("log_tag", "Error parsing data " + e.toString());
      }
      }
      catch (Exception e)
      {
          e.printStackTrace();
          Log.i("erreur : ", e.toString());
      }
    
    }

Error:

05-28 08:02:22.511: I/TAG(2352): je suis là x
05-28 08:02:25.520: W/KeyCharacterMap(2352): No keyboard for id 0
05-28 08:02:25.520: W/KeyCharacterMap(2352): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-28 08:02:27.620: I/TAG(2352): je suis là p1
05-28 08:02:27.620: I/TAG(2352): je suis là p2
05-28 08:02:27.650: I/TAG(2352): je suis là p3
05-28 08:02:27.660: I/TAG(2352): je suis là p4
05-28 08:02:27.680: I/TAG(2352): je suis là pp5
05-28 08:02:27.740: D/dalvikvm(6848): GC_EXPLICIT freed 199K, 52% free 2647K/5447K, external 509K/517K, paused 1736ms
05-28 08:02:31.250: D/dalvikvm(167): GC_EXPLICIT freed 63K, 55% free 2968K/6535K, external 1726K/2233K, paused 157ms
05-28 08:02:37.851: W/System.err(2352): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <html>@2:7 in java.io.InputStreamReader@40589940) 
05-28 08:02:37.881: W/System.err(2352):  at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273)
05-28 08:02:37.881: W/System.err(2352):  at org.kxml2.io.KXmlParser.require(KXmlParser.java:1424)
05-28 08:02:37.921: W/System.err(2352):  at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:127)
05-28 08:02:37.921: W/System.err(2352):  at org.ksoap2.transport.Transport.parseResponse(Transport.java:90)
05-28 08:02:37.931: W/System.err(2352):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:179)
05-28 08:02:37.931: W/System.err(2352):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:90)
05-28 08:02:37.931: W/System.err(2352):  at com.sia.barcode.gui.SearchActivity.soap(SearchActivity.java:85)
05-28 08:02:37.931: W/System.err(2352):  at com.sia.barcode.gui.SearchActivity.doSearch(SearchActivity.java:102)
05-28 08:02:37.982: W/System.err(2352):  at java.lang.reflect.Method.invokeNative(Native Method)
05-28 08:02:37.982: W/System.err(2352):  at java.lang.reflect.Method.invoke(Method.java:507)
05-28 08:02:38.005: W/System.err(2352):  at android.view.View$1.onClick(View.java:2139)
05-28 08:02:38.005: W/System.err(2352):  at android.view.View.performClick(View.java:2485)
05-28 08:02:38.031: W/System.err(2352):  at android.view.View$PerformClick.run(View.java:9080)
05-28 08:02:38.031: W/System.err(2352):  at android.os.Handler.handleCallback(Handler.java:587)
05-28 08:02:38.031: W/System.err(2352):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-28 08:02:38.031: W/System.err(2352):  at android.os.Looper.loop(Looper.java:123)
05-28 08:02:38.031: W/System.err(2352):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-28 08:02:38.061: W/System.err(2352):  at java.lang.reflect.Method.invokeNative(Native Method)
05-28 08:02:38.071: W/System.err(2352):  at java.lang.reflect.Method.invoke(Method.java:507)
05-28 08:02:38.071: W/System.err(2352):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-28 08:02:38.071: W/System.err(2352):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-28 08:02:38.101: W/System.err(2352):  at dalvik.system.NativeStart.main(Native Method)
05-28 08:02:38.101: I/erreur :(2352): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <html>@2:7 in java.io.InputStreamReader@40589940) 
05-28 08:02:44.180: W/ProcessStats(62): Skipping unknown process pid 7130
05-28 08:02:44.200: W/ProcessStats(62): Skipping unknown process pid 7131

***************************************************************************************************************************************************************************************************
my webservice WSproduits.wsdl :

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 U (http://www.xmlspy.com) by any (Ru-Board) -->
<!-- partie 1 : Definitions -->
<definitions xmlns:typens="urn:WSproduits" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:WSproduits" name="Android">
<wsdl:types>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:WSproduits">
</xsd:schema>
</wsdl:types>
<message name="getProductsRequest">
<part name="ean" type="xsd:string"/>
</message>
<message name="getProductsResponse">
<part name="return" type="xsd:string"/>
</message>
<portType name="AndroidPort">
<!-- partie 5 : Operation -->
<operation name="getProducts">
<input message="typens:getProductsRequest"/>
<output message="typens:getProductsResponse"/>
</operation>
</portType>
<binding name="WSproduitsSOAP" type="" typens="WSproduits">
<soap:binding transport="http://schemas.org/soap/http"/>
<operation name="getProducts">
<soap:operation soapAction="urn:WSproduits/getProducts"/>
<input>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:WSproduits"/>
</input>
<output>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:WSproduits"/>
</output>
</operation>
</binding>
<service name="AndroidService">
<documentation>Web service pour notre projet Android </documentation>
<!-- partie 8 : Port -->
<binding name=" AndroidBinding " type=" typens : AndroidPort ">
</binding>
<port name="AndroidPort" binding="typens:AndroidBinding">
<soap:address location="http://wsproducts.local/WebserviceProduit/WSproduits.php"/>
</port>
</service>
<!-- partie 2 : Types-->
<!-- partie 3 : Message -->
<!-- partie 4 : Port Type -->
<!-- partie 6 : Binding -->
<!-- partie 7 : Service -->
</definitions>

*************************************************************************************************************************************************************************************************** 

My server : WSproduits.php

<?php
ini_set("soap.wsdl_chache_enabled",0);
$serveurSOAP = new SoapServer("WSproduits.wsdl");
$serveurSOAP->addFunction('getProducts');
$serveurSOAP->handle();

function getProducts($ean){
try{
mysql_connect("localhost","root","");
mysql_select_db("Produit");
$sql=mysql_query("select * from produit where ID_Produit='".$ean."' ");
while($row=mysql_fetch_array($sql))
$output[]=$row;
}
catch(Exception $e){echo $e;}
$return = json_encode($output);
return $return;
}
?>

You can test this code with KSOAP and ANDROID Emulator, without BD just return "hello message"
thank you in advance

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate