Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I created web service and I am using http://localhost:8080/wenservice/services/Connectionserver?wsdl link in my android app for fetching data. I want to fetch that data into my dynamic list view. I use Jsonobject. So how can I achieve this from wsdl URL dynamically. please guide me. i did following coding for that

MainActivity.java

package com.example.androidjson;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private ProgressDialog pDialog;

// URL to get contacts JSON
private static String url = "http://localhost:8080/wenservice/services/Connectionserver?wsdl";
// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<hashmap><string,>> contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<hashmap><string,>>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView // getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
// Starting single contact activity
Intent in = new Intent(getApplicationContext(),
SingleClassActivity.class);
in.putExtra(TAG_NAME, name);
startActivity(in);}});
// Calling async task to get json
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<void,> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// tmp hashmap for single contact
HashMap<string,> contact = new HashMap<string,>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();}
} else {
Log.e("ServiceHandler", "Couldn't get any datafrom the url");
}return null;}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();

ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[] {
TAG_NAME }, new int[] { R.id.name });
setListAdapter(adapter); }}}

ServiceHandler.java

package com.example.androidjson;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class ServiceHandler
{static String responce = null;
public final static int GET=1;
public final static int POST=2;
public ServiceHandler(){}
//making service call
public String makeServiceCall(String url,int method)
{return this.makeServiceCall(url, method);}
public String makeServiceCall(String url, int method, List<namevaluepair> params)
{try {// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(newUrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);}
httpEntity = httpResponse.getEntity();
String response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();}
return url;}}
SingleClassActivity.java
package com.example.androidjson;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SingleClassActivity extends Activity
{ //JSOn Node Array
private static final String TAG_ID="id";
private static final String TAG_NAME="name";
@Override
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_contact);
Intent in = getIntent();
//get json value from prev intent
String id = in.getStringExtra(TAG_ID);
String name= in.getStringExtra(TAG_NAME);
// display all values on screen
TextView lblId = (TextView) findViewById(R.id.id_label);
TextView lblName = (TextView) findViewById(R.id.name_label);
lblId.setText(id);
lblName.setText(name);}}

activity_main.xml


<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"> android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<listview> android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
activity_single_contact.xml

<linearlayout> xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<!-- ID Label -->
<textview android:id="@+id/id_label" xmlns:android="#unknown">
android:layout_width="fill_parent"
android:layout_height="wrap_removed"
android:textSize="25dip"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textColor="#43bd00"/>
<!-- Name Label -->
<textview android:id="@+id/name_label">
android:layout_width="fill_parent"
android:layout_height="wrap_removed"
android:textSize="25dip"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textColor="#43bd00"/>
list_item.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"> android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<!-- id Label -->
<textview>
android:id="@+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_removed"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold" />
<!-- Email label -->
<textview>
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="#acacac" />
<uses-permission android:name="android.permission.INTERNET" xmlns:android="#unknown">

There is no any error in program but when I run it shows me force to close application.and problem with doInbackground method. What I did Wrong here? Is there any other method to deal with wsdl URL link. plz guide me.

Thank you.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900