在Android中传递参数的URL [英] passing parameters to url in android
本文介绍了在Android中传递参数的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开发一个Android的application.I要传递的数据网址 http://services.mascus.com/api/mascusapi.asmx?op=AddContactRequest 我已经使用POSTDATA方法。 下面是code我已经申请
I am developing an android application.I want to pass data to url http://services.mascus.com/api/mascusapi.asmx?op=AddContactRequest I have used postdata method. Below is the code i have applied
package com.xib;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
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 android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class TestHttpPost extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
postData();
}
public void postData() {
Log.d("hi", "value of array is ");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("//http://services.mascus.com/api/mascusapi.asmx?op=OpenSession");
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "xib"));
nameValuePairs.add(new BasicNameValuePair("password", "efi99LKW"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("hi", "value of array is ");
HttpResponse response = httpclient.execute(httppost);
Log.e("hi", "value of array is ->"+convertStreamToString(response.getEntity().getContent()));
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public String convertStreamToString(InputStream is)
throws IOException {
/*
* To convert the InputStream to String we use the
* Reader.read(char[] buffer) method. We iterate until the
* Reader return -1 which means there's no more data to
* read. We use the StringWriter class to produce the string.
*/
if (is != null)
{
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
}
在此先感谢 图莎尔
Thanks in advance Tushar
推荐答案
如果你想调用的机器人,你可以使用Ksoap2肥皂方法 <一href="http://stackoverflow.com/questions/1052300/how-to-call-a-net-webservice-from-android-using-ksoap2">How使用KSOAP2从Android的调用.NET web服务?
If you want to invoke soap methods from android you can use Ksoap2 How to call a .NET Webservice from Android using KSOAP2?
这篇关于在Android中传递参数的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文