Hallo zusammen.
Ich würde gerne von meinem Android Gerät auf eine Externe Datenbank zugreifen,
Spetzifikationen:
-Aufbau der App mit Fragmente
-Listview mit ("item und Sub item")
Kann mir jemand helfen?
Gibt es für das andwenden von MySql und Fragmenten ein Tutorial?
Edit:
Ich habe nun daran gearbeitet und bin auf folgendes gekommen (bis jetzt)
Spoiler anzeigen
[/spoiler ]
Jedoch erhalte ich hier:
Einen Fehler Kann mir da jemand helfen?
Ich würde gerne von meinem Android Gerät auf eine Externe Datenbank zugreifen,
Spetzifikationen:
-Aufbau der App mit Fragmente
-Listview mit ("item und Sub item")
Kann mir jemand helfen?
Gibt es für das andwenden von MySql und Fragmenten ein Tutorial?
Edit:
Ich habe nun daran gearbeitet und bin auf folgendes gekommen (bis jetzt)
Java-Quellcode
- import android.content.Context;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by Roger on 11.03.2015.
- */
- public class menu4_Fragment extends Fragment {
- View rootview;
- private String jsonResult;
- private String url = "Link Zur PHP Datei";
- private ListView listView;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- rootview = inflater.inflate(R.layout.menu4_layout, container, false);
- final ListView listview = (ListView) rootview.findViewById(R.id.listView1);
- accessWebService();
- return rootview;
- }
- // Async Task to access the web
- private class JsonReadTask extends AsyncTask<String, Void, String> {
- @Override
- protected String doInBackground(String... params) {
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost(params[0]);
- try {
- HttpResponse response = httpclient.execute(httppost);
- jsonResult = inputStreamToString(
- response.getEntity().getContent()).toString();
- }
- catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- private StringBuilder inputStreamToString(InputStream is) {
- String rLine = "";
- StringBuilder answer = new StringBuilder();
- BufferedReader rd = new BufferedReader(new InputStreamReader(is));
- try {
- while ((rLine = rd.readLine()) != null) {
- answer.append(rLine);
- }
- }
- catch (IOException e) {
- // e.printStackTrace();
- }
- return answer;
- }
- @Override
- protected void onPostExecute(String result) {
- ListDrwaer();
- }
- }// end async task
- public void accessWebService() {
- JsonReadTask task = new JsonReadTask();
- // passes values for the urls string array
- task.execute(new String[] { url });
- }
- // build hash set for list view
- public void ListDrwaer() {
- List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
- try {
- JSONObject jsonResponse = new JSONObject(jsonResult);
- JSONArray jsonMainNode = jsonResponse.optJSONArray("emp_info");
- for (int i = 0; i < jsonMainNode.length(); i++) {
- JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
- String name = jsonChildNode.optString("employee name");
- String number = jsonChildNode.optString("employee no");
- String outPut = name + "-" + number;
- employeeList.add(createEmployee("employees", outPut));
- }
- } catch (JSONException e) {
- }
- SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), mylist,
- android.R.layout.simple_list_item_1,
- new String[] { "employees" }, new int[] { android.R.id.text1 });
- listView.setAdapter(simpleAdapter);
- }
- private HashMap<String, String> createEmployee(String name, String number) {
- HashMap<String, String> employeeNameNo = new HashMap<String, String>();
- employeeNameNo.put(name, number);
- return employeeNameNo;
- }
- }
Jedoch erhalte ich hier:
Einen Fehler Kann mir da jemand helfen?
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „schnibli“ ()