2013-02-26 4 views
1

그래서 로그인 활동을 시도하고 있습니다. 그래서 PHP webservice 1login.php 만들었습니다 MySQL에서 데이터베이스를 사용합니다. 그런 다음 makeHttpRequest (url, method, params) 메서드 및 LoginActivity 클래스가있는 JSONParser 클래스가 있습니다. 그러나 문제가 있습니다. 나는 webservice에 매개 변수를 보낼 수 없지만 GET 메서드로 시도했을 때 (char '@와 함께 문제가 발생했습니다.)'작동했습니다. Post 메서드를 사용하는 방법을 알고 싶습니다.게시 방법이 안드로이드에서 작동하지 않습니다.

PHP : (이 잘 작동, 그래서 문제가 여기에있다 생각하지 않는다)

<?php 

include "0conn.php"; 
$email =$_POST["email"]; 
$heslo =$_POST["heslo"]; 
$pass=sha1($heslo); 
$query = mysqli_query($conn,"SELECT jmeno,email FROM users WHERE email='$email' AND heslo='$pass'"); 
$res=array(); 
$response=array(); 
if(mysqli_num_rows($query)==1){ 
$result=mysqli_fetch_array($query); 
$res["vysledek"]=1; 
$res["email"]=$result["email"]; 
$res["jmeno"]=$result["jmeno"]; 
} 
else $res["vysledek"]=0; 
$response[]=$res; 
mysqli_free_result($query); 
echo json_encode($response); 
?> 

JSONParser 클래스 : 여기 코드는

package com.thevnkid93.ucebnice; 


    import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
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.HttpClient; 
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.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.R.integer; 
import android.os.AsyncTask; 
import android.util.Log; 

    public class JSONParser{ 

     static InputStream is = null; 
     static JSONObject jObj = null; 
     static JSONArray jArr=null; 
     static String json = ""; 




     // function get json from url 
     // by making HTTP POST or GET method 
     public static JSONArray makeHttpRequest(String url, String method, 
       List<NameValuePair> params) { 

      // Making HTTP request 
      try { 
       // check for request method 
       if(method.equals("POST")){ 
        // request method is POST 
        // defaultHttpClient 

        DefaultHttpClient httpClient = new DefaultHttpClient(); 
        HttpPost httpPost = new HttpPost(url); 
        httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8")); 

        HttpResponse httpResponse = httpClient.execute(httpPost); 
        HttpEntity httpEntity = httpResponse.getEntity(); 
        is = httpEntity.getContent(); 

       }else if(method.equals("GET")){ 
        // request method is GET 
        DefaultHttpClient httpClient = new DefaultHttpClient(); 
        String paramString = URLEncodedUtils.format(params, "utf-8"); 
        url += "?" + paramString; 
        HttpGet httpGet = new HttpGet(url); 

        HttpResponse httpResponse = httpClient.execute(httpGet); 
        HttpEntity httpEntity = httpResponse.getEntity(); 
        is = httpEntity.getContent(); 
       }   

      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      try { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(
         is, "iso-8859-1"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       json = sb.toString(); 
      } catch (Exception e) { 
       Log.e("Buffer Error", "Error converting result " + e.toString()); 
      } 

      // try parse the string to a JSON object 
      try { 
       jArr = new JSONArray(json); 
      } catch (JSONException e) { 
       Log.e("JSON Parser", "Error parsing data " + e.toString()); 
      } 
      // return JSON String (Array) 
      return jArr; 
     } 


} 

과 LoginActivity 클래스 :

package com.thevnkid93.ucebnice; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.concurrent.ExecutionException; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

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

public class LoginActivity extends Activity{ 

    EditText e1,e2; 
    Button b1,b2; 
    List<NameValuePair>infoPost=new ArrayList<NameValuePair>(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login_layout); 
     e1 = (EditText)findViewById(R.id.login_e1); 
     e2 = (EditText)findViewById(R.id.login_e2); 
     b1 = (Button)findViewById(R.id.login_b1); 
     b2 = (Button)findViewById(R.id.login_b2); 

     b1.setOnClickListener(new OnClickListener(){ 

      public void onClick(View arg0){ 
        int vys=0; 
        String email=e1.getText().toString(); 
        String heslo=e2.getText().toString(); 
        LoadInfo loadi=new LoadInfo(); 
        try { 
         vys=loadi.execute(email,heslo).get(); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (ExecutionException e) { 
         Toast.makeText(LoginActivity.this, "xxxxxxxx", Toast.LENGTH_LONG).show(); 
         e.printStackTrace(); 
        } 

        Toast.makeText(LoginActivity.this, ""+vys, Toast.LENGTH_LONG).show(); 
      } 
     });//TLACITKO PRIHLASENI 




    } 

} 

class LoadInfo extends AsyncTask<String, String, Integer>{ 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    @Override 
    protected Integer doInBackground(String... args) { 
     params.add(new BasicNameValuePair("email", args[0])); 
     params.add(new BasicNameValuePair("heslo", args[1])); 
     JSONArray jArray = JSONParser.makeHttpRequest(my_url, "POST", params); 
     int vys=0; 
     try { 
      JSONObject jobject = jArray.getJSONObject(0); 
      vys=jobject.getInt("vysledek"); 
     } catch (JSONException e) { 
      vys=2; 
      e.printStackTrace(); 
     } 
     return vys; 
    } 


} 

답변

0

가장 먼저. 더 구체적인 정보가 필요하다 "그냥 작동하지 않는다"

해야 할 일.

1) 멋진 자습서를 사용하십시오. this을 강력하게 권합니다.

2) 코드를 확인하기 전에 PHP가 작동하는지 확인하십시오. 굉장한 것이있다! 테스터 POSTman. 그것으로 PHP 코드를 테스트하십시오.

3) 수업을 체계적으로 구성하십시오. 내가 만든 튜토리얼처럼 웹 호출을 처리하는 useractions 클래스와 사용자가 만든 JSON 클래스에 대한 호출을 만들어야한다.

이러한 세 가지로 코드를 디버그 한 다음 커뮤니티에서 가장 도움이 될 수있는 특정 문제를 제공 할 수 있습니다.

+0

문제는 websevice가 작동하지 않는다는 것이 었습니다. 새로운 웹 사이트를 만들었고 지금은 잘 작동합니다. – vnkid

관련 문제