Hello all,
I'm trying to consume WCF service in android client but I'm getting following error :
java.lang.string cannot be converted to jsonobject
here is my code :
Android :
package com.example.studentsapp;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.view.View.OnClickListener;
public class MainActivity extends Activity
{
private final static String srvc_uri = "http://101.63.53.242/StudentWcfServiceforAndroid/service1.svc/GetName/";
private EditText idTxt;
private Button btn;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idTxt = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(srvc_uri + idTxt.getText());
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if(entity.getContentLength() != 0)
{
Reader reader = new InputStreamReader(response.getEntity().getContent());
char[] buffer = new char[(int) response.getEntity().getContentLength()];
reader.read(buffer);
reader.close();
JSONObject s = new JSONObject(new String(buffer));
tv.setText(s.getString("StudentName"));
}
}
catch(Exception ex)
{
Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
WCF :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace StudentWcfServiceforAndroid
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
[ServiceContract]
public interface IstudentInfo
{
[OperationContract]
[WebGet(UriTemplate = "GetName/{id}",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
Student GetStudentName(int studentID);
}
public class StudentInfo : IstudentInfo
{
public Student GetStudentName(int studentID)
{
Student studs = GetStuds().Where(s => s.StudentID == studentID).FirstOrDefault();
return studs;
}
private List<Student> GetStuds()
{
return new List<Student>
{
new Student
{StudentID=1, StudentName="AAA"},
new Student
{StudentID=2, StudentName="BBB"}
};
}
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
[DataContract]
public class Student
{
public int StudentID
{ get; set; }
public string StudentName
{ get; set; }
}
}
Web.config :
="1.0"
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="StudentWcfServiceforAndroid.StudentInfo">
<endpoint kind="webHttpEndpoint" contract="StudentWcfServiceforAndroid.IStudentInfo" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Help me to get rid of this.
Now I've solved this problem but I'm neither getting any error nor output !!