بابت پاسختان واقعا ممنون
یک هفتی اوی ی سایت خارجی مطرح کردم ولی جوابی تشتیدم
من ی کد نوشتم رشته را ذخیره کنم ولی اررور میده
http://stackoverflow.com/questions/28169...to-sqllite
اگه میشه یه نگاهی بندازین.
این هم کد
یک هفتی اوی ی سایت خارجی مطرح کردم ولی جوابی تشتیدم
من ی کد نوشتم رشته را ذخیره کنم ولی اررور میده
http://stackoverflow.com/questions/28169...to-sqllite
اگه میشه یه نگاهی بندازین.
این هم کد
نقل قول:public class MainActivity extends ListActivity
InputStream is = null;
String ip = "http://192.168.56.1/2.txt";
String line = null;
String result = null;
public String[] list1, list2;
SQLiteDB sqlite_obj;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sqlite_obj = new SQLiteDB(MainActivity.this);
try {
URL url = new URL(ip);
executeReq(url);
webserviceCall();
Toast.makeText(getBaseContext(), "Data from Webservice", Toast.LENGTH_SHORT).show();
}
catch(Exception e) {
sqlitecall();
Toast.makeText(getBaseContext(), "Data from SQLite DB", Toast.LENGTH_SHORT).show();
}
setListAdapter(new ListViewAct(this, list1, list2));
}
private void executeReq(URL url) throws IOException {
// TODO Auto-generated method stub
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(3000);
con.setConnectTimeout(3500);
con.setRequestMethod("GET");
con.setDoInput(true);
// Connect
con.connect();
}
private void webserviceCall() {
// TODO Auto-generated method stub
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(ip);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
catch (Exception e) {
Log.e("Webservice 1", e.toString());
}
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("Webservice 2", e.toString());
}
try {
JSONArray ja = new JSONArray(result);
JSONObject jo = null;
list1 = new String[ja.length()];
list2 = new String[ja.length()];
for(int i=0; i<ja.length(); i++) {
jo = ja.getJSONObject(i);
list1[i] = jo.getString("id");
list2[i] = jo.getString("name");
}
}catch (Exception e) {
Log.e("Webservice 3", e.toString());
}
sqlite_obj.open();
sqlite_obj.deleteAll();
for(int i=0; i<list1.length; i++) {
sqlite_obj.insert(list1[i].toString(), list2[i].toString());
}
sqlite_obj.close();
}
private void sqlitecall() {
// TODO Auto-generated method stub
sqlite_obj.open();
Cursor c = sqlite_obj.getAllData();
list1 = new String[c.getCount()];
list2 = new String[c.getCount()];
if (c.moveToFirst())
{
int i=0;
do {
DisplayContact(c, i);
i++;
} while (c.moveToNext());
}
sqlite_obj.close();
}
private void DisplayContact(Cursor c, int i) {
// TODO Auto-generated method stub
list1[i] = c.getString(0);
list2[i] = c.getString(1);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}