2011-02-04 22 views
1

http postを使用してasp.netのGenericハンドラ.ashxページにアンドロイドからデータを送信しています。しかし、ハンドラはデータを受け取ることができません。事はHTTPGETで動作しなくhttppostasp.netでアンドロイドからashxに送信されたhttp投稿要求。データを受信できません

とアンドロイドのコード

package com.postApp; 
/* 
* HTTP POST and BasicNameValuePair 
* */ 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
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.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class postAct extends Activity { 
    /** Called when the activity is first created. */ 

    class login{ 
     public 
     String uname; 
     public String pass; 

    } 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


      login l=new login(); 


     HttpClient client1 = new DefaultHttpClient(); 
     HttpPost request = new HttpPost("http://10.0.2.2:18089/POC_login/Handler.ashx");  


     l.uname="piyush"; 
     l.pass="gupta"; 
       List<NameValuePair> postParameters = new ArrayList<NameValuePair>(3); 
      postParameters.add(new BasicNameValuePair("uname", l.uname)); 
      postParameters.add(new BasicNameValuePair("pass", l.pass)); 
     try { 
      request.setEntity(new UrlEncodedFormEntity(postParameters)); 
      UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 
      request.setEntity(formEntity); 

      HttpResponse response; 
      response = client1.execute(request); 

     BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
     String line; 
     String page=""; 
     line = in.readLine(); 
     while(line!=null) 
     { 
      page=page+line; 
      line=in.readLine(); 
     } 
     TextView tv = (TextView) findViewById(R.id.textview); 
     tv.setText(page); 
     in.close(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }      
    } 


} 

ASHXコード

<%@ WebHandler Language="C#" Class="Handler" %> 

using System; 
using System.Web; 
using System.Linq; 

public class Handler : IHttpHandler { 

    public void ProcessRequest (HttpContext context) 
    { 
     context.Request.ContentType = "text/plain"; 
     // context.Request.ContentType = "text/html"; 


     string username=context.Request.QueryString["uname"]; 
     string password = context.Request.QueryString["pass"]; 
     context.Response.Write("Hello Piyush"); 

     NorthwindDataContext db = new NorthwindDataContext(); 
     var found = (from p in db.Catergories 
        where p.cat_ID == 1 
        select p.cat_name).SingleOrDefault(); 

    } 

    public bool IsReusable { 
     get { 
      return false; 
     } 
    } 

} 

助けてください!

+0

あなたはエラーを取得していますか?返品ステータスコードは何ですか?何が起きているのかを理解するために、いくつかのデバッグステートメントをコードに追加することをお勧めします。 – dbryson

+0

ご質問ありがとうございます、私はどのようにウェブでの投稿要求を処理するかを学んでいます、それはあなたのためにこのコードの下で働いていますか? 'context.Request.Form [" supplier_name "];' context.Request.Form 'に変更しました。 – Megamind

答えて

1

問題は、コンテンツタイプの誤った取り扱いによるものと考えられます。

のAndroid側では

、あなたは例えば、Content-Typeヘッダを設定する必要があります。

request.addHeader("Content-type", "application/x-www-form-urlencoded"); 
HttpResponse response; 
response = client1.execute(request); 

そして、あなたは、あなたのサーバー上のコンテンツ・タイプをオーバーライドしてはならない、すなわちのprocessRequestから以下を削除します。

context.Request.ContentType = "text/plain"; 
// context.Request.ContentType = "text/html"; 

問題が解決しない場合は、サーバーがPOSTを受信するように設定されていることを確認してください。 uはASHX使用

は投稿しないGET呼び出すとき

関連する問題