-2
我在android.How傳遞參數的URL我知道,我已經通過了值是否被髮送到url.Below是我的Java代碼
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 "";
}
}
}
誰能幫我 臨屋提前NKS 圖莎爾
你問了同樣的問題[再次](http://stackoverflow.com/questions/5349239/passing-parameters-to-url-in-android)。不要這樣做。我認爲你已經[禁止之前](http://stackoverflow.com/users/553117/user553117)。這是你製作的[另一個帳戶](http://stackoverflow.com/users/593694/tushar)。 – Reno 2011-03-18 09:45:19