2016-05-31 24 views
0

Hier bin ich holen SMS vom Telefon und lade 10 SMS auf dem Server alle Code funktioniert gut, aber statt 10 SMS nur auf Server hochladen Spezifische eine SMS hochgeladen 10 Mal auf Server, pls sag mir was Ich vermisse meinen Code?Holen Sie SMS Vom Telefon und laden Sie auf Server

Hier ist mein Message_class.Java-Code.

 public class message_class extends Activity{ 
int j = 0; 
Button btninbox; 

ListView lstView; 

SimpleCursorAdapter adapter; 

ArrayList<Message_Item> msg_list; 

String Str_Msg, Str_Phone,dated; 

Msg_adapter msg_adapter; 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.msginbox_layout); 

    msg_list = new ArrayList<Message_Item>(); 
    btninbox = (Button) findViewById(R.id.btn_inbox); 
    btninbox.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 


       Intent intent = new Intent(message_class.this, Msg_Recive.class); 
       startActivity(intent); 

     } 
    }); 



    lstView = (ListView) findViewById(R.id.lv_msg); 

    fetchInbox(); 




      final int arraysize = msg_list.size(); 


      for (int j=0; j<10;j++){ 

       Str_Msg = msg_list.get(j).getStrMsg().toString(); 
       Str_Phone = msg_list.get(j).getStrNumber().toString(); 
       Toast.makeText(message_class.this, Str_Phone+" "+Str_Msg, Toast.LENGTH_LONG).show(); 


      new HttpAsyncTask() 
        .execute("http://demo.glowsosl.com/synchs_dsda_app/insert_details_msg.php"); 



      msg_adapter.notifyDataSetChanged(); 
      } 



} 



private class HttpAsyncTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 

     Contacts person = new Contacts(); 
     person.setPhone(Str_Phone); 
     person.setName(Str_Msg); 


     return POST(urls[0], person); 



    } 

    // onPostExecute displays the results of the AsyncTask. 
    @Override 
    protected void onPostExecute(String result) { 

     Toast.makeText(getBaseContext(), result + "Data Sent!", 
       Toast.LENGTH_LONG).show(); 



    } 
} 

private static String convertInputStreamToString(InputStream inputStream) 
     throws IOException { 
    BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(inputStream)); 
    String line = ""; 
    String result = ""; 
    while ((line = bufferedReader.readLine()) != null) 
     result += line; 

    inputStream.close(); 
    return result; 

} 

public static String POST(String url, Contacts person) { 
    InputStream inputStream = null; 
    String result = ""; 
    try { 

     // 1. create HttpClient 
     HttpClient httpclient = new DefaultHttpClient(); 

     // 2. make POST request to the given URL 
     HttpPost httpPost = new HttpPost(url); 

     String json = ""; 

     // 3. build jsonObject 
     JSONObject jsonObject = new JSONObject(); 
     jsonObject.accumulate("contact_no", person.getPhone()); 
     jsonObject.accumulate("sim_num", "Unknown"); 
     jsonObject.accumulate("msg", person.getName()); 

     // 4. convert JSONObject to JSON to String 
     json = jsonObject.toString(); 



     // 5. set json to StringEntity 
     StringEntity se = new StringEntity(json); 

     // 6. set httpPost Entity 
     httpPost.setEntity(se); 

     // 7. Set some headers to inform server about the type of the 
     // content 
     httpPost.setHeader("Accept", "application/json"); 
     httpPost.setHeader("Content-type", "application/json"); 

     // 8. Execute POST request to the given URL 
     HttpResponse httpResponse = httpclient.execute(httpPost); 

     // 9. receive response as inputStream 
     inputStream = httpResponse.getEntity().getContent(); 

     // 10. convert inputstream to string 
     if (inputStream != null) 
      result = convertInputStreamToString(inputStream); 
     else 
      result = "Did not work!"; 

    } catch (Exception e) { 
     Log.d("InputStream", e.getLocalizedMessage()); 
    } 

    // 11. return result 
    return result + "," + person.getName() + "," 
      + person.getPhone(); 
} 

ArrayList<String> jsonStringToArray(String jsonString) throws JSONException { 

    ArrayList<String> stringArray = new ArrayList<String>(); 

    JSONArray jsonArray = new JSONArray(jsonString); 

    for (int i = 0; i < jsonArray.length(); i++) { 
     stringArray.add(jsonArray.getString(i)); 
    } 

    return stringArray; 

} 

public void fetchInbox() { 
    // ArrayList sms = new ArrayList(); 

    Uri uriSms = Uri.parse("content://sms/inbox"); 
    Cursor cursor = getContentResolver().query(uriSms, 
      new String[] { "_id", "address", "date", "body" }, null, null, 
      null); 
    //for (int i =0; i<((JSONArray) cursor).length();i++){ 
     //Toast.makeText(getApplicationContext(), "work", Toast.LENGTH_SHORT).show(); 
    //} 
    cursor.moveToFirst(); 
    while (cursor.moveToNext()) { 
     String address = cursor.getString(1); 
     String date = cursor.getString(2); 
     String body = cursor.getString(3); 

     // Toast.makeText(getApplicationContext(), cursor.getString(2), Toast.LENGTH_SHORT).show(); 

     msg_list.add(new Message_Item(address, body,date)); 

    } 

    msg_adapter = new Msg_adapter(msg_list, message_class.this); 
    lstView.setAdapter(msg_adapter); 

} 

Antwort

1

Anstatt die asynchrone Aufgabe der Schleifenseite anzuwenden, verwenden Sie sie in der doInBackground-Methode.

Ihre DoInBackground-Methode wird wie folgt aussehen.

@Override 
protected String doInBackground(String... urls) { 
    String result=""; 
    for(int i = 0; i < msg_list.size(); i++){ 
     Str_Msg = msg_list.get(i).getStrMsg().toString(); 
     Str_Phone = msg_list.get(i).getStrNumber().toString(); 

     Contacts person = new Contacts(); 
     person.setPhone(Str_Phone); 
     person.setName(Str_Msg); 
     result= result + POST(urls[0], person); 
    } 
    return result; 
} 

Und diese asynchrone Aufgabe nur einmal aufrufen.

Viel Spaß !!

+0

danke seine Arbeit für mich – user6405383

Verwandte Themen