2011-12-06 50 views
0

我有一個應用程序,使用Zxing條碼掃描儀掃描庫存資產(電腦,設備) 我可以返回到一個文本字段的條形碼。然後,我可以在窗體上的其他編輯文本字段(description,model,serial#)中爲該特定掃描資產輸入更多數據。下面的按鈕用於在另一個活動的ListView中將數據保存到我的SQLite數據庫。我想知道,如果我可以點擊列表視圖中的某個項目並將其內容發送到mysql服務器。我似乎在網絡上的一些信息(谷歌),但有沒有人知道任何好的教程或步行通過,可以指導我。點擊ListView(安卓)項目後mySQL遠程學校服務器

感謝,

回答

1

要做到這一點,你一般會運行,從您的應用程序接受數據並將其插入到MySQL數據庫的Web服務。您可以使用任何服務器端腳本語言編寫Web服務,但大多數示例都使用PHP。

欲瞭解更多信息,我建議看這些類似的問題:

How to connect to a remote MySQL DB without webservices... ? (ANDROID)

Android remote MySQL operations using a web service with php

也許你會找到自己感興趣的Kumulos,它是一個工具,我已經參與了建築,允許您創建這些類型的Web服務並將它們集成到您的移動應用程序中。

+0

嘿,感謝您的信息...我我寫了這個昨天后掉了....累了從我的應用程序編碼...我知道了它,並儘管...只是這最後一小部分...我使用TeamViewer,我有一些示例PHP代碼使用...我試圖看到的是如何在android中編寫代碼,在我的listview活動中的實際數據...感謝鏈接...今晚我會徹底閱讀它們一個解決方案... –

+0

啊,好吧,對於稍微誤解你的事後抱歉,祝你好運找到一個辦法! – cgwyllie

+0

沒關係......我找到了一個臨時解決方案。我在我的應用程序的按鈕上添加了一個mysql按鈕和其他保存按鈕。我研究了一些使用json和java的編碼,並創建了一個json對象來保存我的程序中的四個元素,編寫了我的php腳本並在服務器端更新了我的php版本。然後,我得到了一個很好的連接和012-「Wa-la」,數據發佈到服務器......正是我想要的......隨着我對假期的瞭解越來越多,我將添加更多功能......但是,現在完成了應用程序(1.0).... –

0

我能夠通過將所選項目要做到這一點,以onItemClick基於一些代碼,我在網上找到的,希望這可以幫助任何人仍然在努力解決這個

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.HashMap; 
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 org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.henga.beverage.JSONParser; 



public class Songs extends Activity { 
    ListView list; 
    TextView ver; 
    TextView name; 
    TextView api; 
    Button Btngetdata; 

    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>(); 



    //URL to get JSON Array 
    private static String url = "http://api.learn2crack.com/android/jsonos/"; 

    //JSON Node Names 
    private static final String TAG_OS = "android"; 
    private static final String TAG_VER = "ver"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_API = "api"; 

    JSONArray android = null; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
     oslist = new ArrayList<HashMap<String, String>>(); 



     Btngetdata = (Button)findViewById(R.id.getdata); 
     Btngetdata.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       new JSONParse().execute(); 


      } 
     }); 


    } 



    private class JSONParse extends AsyncTask<String, String, JSONObject> { 
     private ProgressDialog pDialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      ver = (TextView)findViewById(R.id.vers); 
      name = (TextView)findViewById(R.id.name); 
      api = (TextView)findViewById(R.id.api); 
      pDialog = new ProgressDialog(Songs.this); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 



     } 

     @Override 
     protected JSONObject doInBackground(String... args) { 

      JSONParser jParser = new JSONParser(); 

      // Getting JSON from URL 
      JSONObject json = jParser.getJSONFromUrl(url); 
      return json; 
     } 
     @Override 
     protected void onPostExecute(JSONObject json) { 
      pDialog.dismiss(); 
      try { 
        // Getting JSON Array from URL 
        android = json.getJSONArray(TAG_OS); 
        for(int i = 0; i < android.length(); i++){ 
        JSONObject c = android.getJSONObject(i); 

        // Storing JSON item in a Variable 
        String ver = c.getString(TAG_VER); 
        String name = c.getString(TAG_NAME); 
        String api = c.getString(TAG_API); 




        // Adding value HashMap key => value 


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

        map.put(TAG_VER, ver); 
        map.put(TAG_NAME, name); 
        map.put(TAG_API, api); 

        oslist.add(map); 
        list=(ListView)findViewById(R.id.list); 





        ListAdapter adapter = new SimpleAdapter(Songs.this, oslist, 
          R.layout.list_songs, 
          new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] { 
            R.id.vers,R.id.name, R.id.api}); 

        list.setAdapter(adapter); 
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

         @Override 
         public void onItemClick(AdapterView<?> parent, View view, 
               int position, long id) { 
          //pb.setVisibility(View.VISIBLE); 

          new MyAsyncTask().execute(oslist.get(+position).get("name")); 
          Toast.makeText(Songs.this, "You Clicked on "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show(); 
          startActivity(new Intent(Songs.this, Main.class)); 
         } 
        }); 

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


     } 
    } 
       private class MyAsyncTask extends AsyncTask<String, Integer, Double>{ 

      @Override 
      protected Double doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      postData(params[0]); 
      return null; 
      } 

      protected void onPostExecute(Double result){ 

      Toast.makeText(getApplicationContext(), "Vote sent", Toast.LENGTH_LONG).show(); 
      } 


      public void postData(String valueIWantToSend) { 
      // Create a new HttpClient and Post Header 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2/z/receiver.php"); 

      try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      @SuppressWarnings("unused") 
      HttpResponse response = httpclient.execute(httppost); 

      } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      } 
     } 

} 
}