2016-06-14 1 views
0

첫 번째 이미지에서 내 두 활동에서 나는 PHP 두 가지 다른 PHP 파일에 요청을 보내고 있으며 나는 안드로이드 활동에 유효한 응답을 받고있다. PHP로 활동 1에서 요청을 보낼 때 두 번째 이미지에서 활동에서 PHP로 데이터를 보내고 동일한 PHP에서 다른 활동으로 데이터를 검색하는 방법은 무엇입니까?

Image 1

, 하나 개 PHP 파일에 모두 PHP 파일 위의 결합에서, 나는 받고 응답 에도 활동 2를 할 수 없습니다입니다 .

private void login(final String textViewNameq, String textViewNameqw,String day) { 

    class LoginAsync extends AsyncTask<String, Void, String> { 

     private Dialog loadingDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading..."); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      String uname = params[0]; 
      String pass = params[1]; 
      String day=params[2]; 

      InputStream is = null; 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("username", uname)); 
      nameValuePairs.add(new BasicNameValuePair("password", pass)); 
      nameValuePairs.add(new BasicNameValuePair("day", day)); 
      String result = null; 

      try { 
       HttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(
         "Your php link goes here"); 
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       HttpResponse response = httpClient.execute(httpPost); 

       HttpEntity entity = response.getEntity(); 

       is = entity.getContent(); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 

       String line = null; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       result = sb.toString(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return result; 
     } 

Image 2 내 PHP 코드는 여기에 간다,

<?php 
include("my php file path");       
// if (!empty($_POST['username']) && !empty($_POST['password'])){ 
     $Fromname = trim($_POST['username']); 
     $Toname = trim($_POST['password']); 
     //$Fromdate=$_POST['iddate']; 
     //$Fromd = date("Y-m-d", strtotime($Fromdate)); 
     $from_id=trim($_POST['idss']); 
     $to_id=trim($_POST['iddd']); 
    // } 

    $url="http://my url with API key goes here"; 


    $headers = array('Content-Type: application/json');     
      $ch = curl_init(); 
       curl_setopt($ch, CURLOPT_URL, $url); 
       curl_setopt($ch, CURLOPT_POST, true); 
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);     
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
       curl_setopt($ch, CURLOPT_ENCODING , "gzip"); 
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);       
       $page = curl_exec($ch); 
       curl_close($ch);  
       $result=json_decode($page,true);  
       if(empty($result)){     
        $page=file_get_contents($url); 
        $result=json_decode($page,true); 
       } 
+0

PHP 태그를 추가 했으므로이 코드는 PHP 코드를 필요로하지 않습니다. 도움이 필요하면 사용중인 프로그래밍 언어로 태그를 편집하십시오. PHP 프로그래머 만 볼 수 있습니다. – KDOT

+0

당신의 PHP 스크립트를 보여주십시오 –

답변

0

같은 PHP 파일에서 필요한 응답을 얻을 수 있습니다. 필요한 것은 요청 방법에 필요한 정보를 보내고 그에 따라 PHP 파일에서 처리하고 응답을 생성하기 만하면됩니다. 우리는 당신이 U. PHP 스크립트를 보여 주면 더 나은 U 자문을 도울 수 있습니다

+0

예 @ Ratul Doley 위의 PHP 스크립트를 추가했습니다. – Kogile

+0

위의 코드가 전체 코드라면 다음과 같이 응답을 생성하지 않습니다. 즉, ur 앱의 내용을 읽지 않습니다. $ result가 응답으로 읽으려는 경우'if (! empty ($ result)) echo $ result; else // some response' –

+0

만약 $ result를 echo하면 첫 번째 이미지와 같은 두 개의 PHP 파일이 있지만 두 번째 이미지와 같은 단일 PHP 파일이 아니라면 응답을 얻을 수 있습니다. – Kogile

관련 문제