필요한 도움의 범위가 확실하지 않지만 방금 올바른 방향을 가리키고 있다고해도 나는 물을 것이라고 생각했습니다.http 입력에서 목록 활동을 표시하는 클래스를 작성하는 데 도움이 필요합니다.
텍스트 상자 및 제출 버튼이있는 기본 레이아웃이 있습니다.
난 hellodatabase
클래스와 함께 텍스트보기로 결과를 retune PHP는 파일에 문자열을 보냅니다.
내가해야 할 일은 문자열을 hellodatabase
클래스로 보내고 결과를 표시하는 것입니다.
다음은 데이터베이스 연결에 사용한 튜토리얼에서 발견 한 클래스입니다.
이 수업을 만드는 데 도움이되거나 좋을 것 같습니다.
package example.hellodatabase;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
public class HelloDatabase extends Activity {
/** Called when the activity is first created. */
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a crude view - this should really be set via the layout resources
// but since its an example saves declaring them in the XML.
LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);
// Set the text and call the connect function.
txt.setText("Connecting...");
//call the method to run the data retreival
txt.setText(getServerData(KEY_121));
}
public static final String KEY_121 = "http://www.mydomain.co.uk/1.php"; //i use my real ip here
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name","beans"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
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();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
//Get an output to the screen
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
}
값을 반복하는 동안 모든 값을 ArrayList에 추가 한 다음 Iterator를 사용하여 String Array로 변환합니다. 사용자 지정 어댑터를 사용하고 목록보기를 부 풀린 다음 텍스트를 설정하거나 모든 Ada 당신이 좋아하는대로 ... list_fname.add (jo.getString ("Value")); string [] VALUE_fname = 새 문자열 [list_fname.size()]; 반복기 it = list_fname.iterator(); while (it.hasNext()) { fn ++; VALUE_fname [fn] = it.next(). toString(); }이 문자열 배열을 어댑터 –
Venky
으로 설정하십시오. Venkatesh는 내가해야 할 일이 무엇인지 이해하고 있지만 지금 당장은 어떻게 작동하는지 많이 알지 못합니다.이 클래스를 작성하면로드에 도움이 될 것입니다. –