2011-03-18 5 views
-2

가능한 중복에 URL에 : 나는 android.How에서 URL에 매개 변수를 전달했습니다
passing parameters to url in android문제는 안드로이드

내가 통과 한 값이 전송 여부를 알 수 있습니다 url.Below 내 자바 코드

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는 Tushar

+0

[질문 다시] (http://stackoverflow.com/questions/5349239/passing-parameters-to-url-in-android) 질문을하셨습니다. 그러지 마. 나는 당신이 [전에 금지되었다]라고 생각한다 (http://stackoverflow.com/users/553117/user553117). 이것은 당신이 만든 [다른 계정] (http://stackoverflow.com/users/593694/tushar)입니다. – Reno

답변

0

그냥 ..

로그인 .... 를 u는 서버에서 응답을받을 것입니다 ..이 줄 끝에서

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

을이 라인

String data = EntityUtils.toString(response.getEntity()); 
    System.out.println("Data..."+data); 

를 추가하고 확인

0
String link_push ="Your link" 
DefaultHttpClient hc_push=new DefaultHttpClient(); 
ResponseHandler <String> res_push=new BasicResponseHandler(); 
HttpPost postMethod_push=new HttpPost(link_push); 
List<NameValuePair> nameValuePairs_push= new ArrayList<NameValuePair>(3); 
nameValuePairs_push.add(newBasicNameValuePair("city_id","0000")));  
    nameValuePairs_push.add(new BasicNameValuePair("user_id","value")); 
    postMethod_push.setEntity(new UrlEncodedFormEntity(nameValuePairs_push)); 
String response_push=hc_push.execute(postMethod_push,res_push); 
Log.v("Response for Push Notification",""+response_push); 

이러한 respones_push에서 서버의 응답을받습니다. .. 시도해보십시오 ..