2013-11-28 48 views
-1

是否可以從android發送字符串變量到php,但將字符串轉換爲整數。因爲當我嘗試將字符串轉換爲int時,出現錯誤,然後將其傳遞給我的php文件。這是代碼。

package com.example.taxamsp; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

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.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.example.taxamsp.extra.alertManager; 
import com.example.taxamsp.extra.connectionDetector; 
import com.example.taxamsp.extra.JSONParser; 

public class subjectInfo extends ListActivity { 
// Connection detector 
connectionDetector cd; 

// Alert dialog manager 
alertManager alert = new alertManager(); 

// Progress Dialog 
private ProgressDialog pDialog; 

// Creating JSON Parser object 
JSONParser jsonParser = new JSONParser(); 

ArrayList<HashMap<String, String>> examlist; 

// tracks JSONArray 
JSONArray exam = null; 

// Subject id 
String subject_id; 

// subjects exam JSON url 
private static final String URL_EXAM = "http://192.168.43.100/tx/subjectexams.php?course="; 

// ALL JSON node names 
private static final String TAG_ID = "id"; 
private static final String TAG_IDCOURSE = "course"; 
private static final String TAG_NAME = "name"; 
private static final String TAG_INTRO = "intro"; 

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

    cd = new connectionDetector(getApplicationContext()); 

    // Check if Internet present 
    if (!cd.isConnectingToInternet()) { 
     // Internet Connection is not present 
     alert.showAlertDialog(subjectInfo.this, "Internet Connection Error", 
       "Please connect to working Internet connection", false); 
     // stop executing code by return 
     return; 
    } 

    // Get subject id 
    Intent i = getIntent(); 
    subject_id = i.getStringExtra(subject_id); 

    // Hashmap for ListView 
    examlist = new ArrayList<HashMap<String, String>>(); 

    // Exams in Background Thread 
    new LoadExams().execute(); 

    // get listview 
    ListView lv = getListView(); 

} 

/** 
* Background Async Task to Load All Exam 
* */ 
class LoadExams extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(subjectInfo.this); 
     pDialog.setMessage("Loading exams ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    /** 
    * getting Subjects JSON 
    * */ 
    protected String doInBackground(String... args) { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 

     // post subject id as GET parameter 
     //params.add(new BasicNameValuePair(subject_id, subject_id)); 

     // getting JSON string from URL 
     String json = jsonParser.makeHttpRequest(URL_EXAM + subject_id, "GET", 
       params); 

     // Check your log cat for JSON reponse 
     Log.d("Subjects JSON: ", "> " + json); 

     try {    
      exam = new JSONArray(json); 

      if (exam != null) { 
       for (int i = 0; i < exam.length(); i++) { 
        JSONObject c = exam.getJSONObject(i); 

        // Storing each json item values in variable 
        String id = c.getString(TAG_ID); 
        String name = c.getString(TAG_NAME); 
        String intro = c.getString(TAG_INTRO); 

        // creating new HashMap 
        HashMap<String, String> map = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        map.put(subject_id, subject_id); 
        map.put(TAG_ID, id); 
        map.put(TAG_NAME, name); 
        map.put(TAG_INTRO, intro); 

        // adding HashList to ArrayList 
        examlist.add(map); 
       } 
      }else{ 
       Log.d("Subjects: ", "null"); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 
    protected void onPostExecute(String file_url) { 
     pDialog.dismiss(); 
     // updating UI from Background Thread 
     runOnUiThread(new Runnable() { 
      public void run() { 
       /** 
       * Updating parsed JSON data into ListView 
       * */ 
       ListAdapter adapter = new SimpleAdapter(
         subjectInfo.this, examlist, 
         R.layout.e_content, new String[] { "subject_id", TAG_ID, TAG_NAME, 
           TAG_INTRO }, new int[] { 
           R.id.subject_id, R.id.quiz_id, R.id.quiz_name, R.id.quiz_intro}); 
       // updating listview 
       setListAdapter(adapter); 
      } 
     }); 

    } 

} 
} 

這是我的php文件。

<?php 

include("config.php"); 
$x = $_GET["subject_id"]; 
    $query = "SELECT id, course, name, intro FROM mdl_quiz WHERE course =".$x.""; 
$result = mysql_query($query); 
$obj = array(); 
$i = 0; 
while($row=mysql_fetch_assoc($result)){ 
    $obj[$i]['id'] = $row["id"]; 
    $obj[$i]['course'] = $row["course"]; 
    $obj[$i]['name'] = $row["name"]; 
    $obj[$i]['intro'] = $row["intro"]; 
    $i++; 
} 

echo json_encode($obj); 
mysql_close(); 


?> 

當我運行它。它在傳遞$ _GET值的部分崩潰並在查詢中使用它。我需要幫助。先謝謝你!

+0

又是一個語法錯誤問題。做得好。 –

+0

嗨,現在我知道這是一個語法錯誤。這就是爲什麼我需要你的幫助才能理解如何正確理解先生@STTLCU :) – user3022926

+0

這不是要求語法錯誤的地方!看到[ask]的指導 –

回答

0

行:

$query = "SELECT id, course, name, intro FROM mdl_quiz WHERE course =".$x.""; 

曾在年底額外的雙引號。

嘗試:

$query = "SELECT id, course, name, intro FROM mdl_quiz WHERE course =".$x."; 

而且,我敢肯定,即使在$ _GET [ 'subject_id']值是數字,即PHP把它像一個字符串。

+0

先生,我不能在查詢結束時使用「」,這是一個語法錯誤。 :) – user3022926

相關問題