2017-03-25 10 views
-1

サーバー上のデータベースを更新しようとしていますが、コードに問題があるようです。コードがcatchブロックに移動し、メッセージがnullのToastが表示されます。メインスレッドからマニフェストAndroidでデータベース値が更新されない

2)Androidのブロックのネットワーク接続へ

public class Test extends AppCompatActivity { 

    static String inputLine, data = ""; 
    HttpURLConnection conn = null; 

    String q = "update data set room='ANKUSH' where id='5'"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test); 

     try { 
      URL url = new URL("http://takeyourtime.16mb.com/postdb.php"); 
      String agent = "Applet"; 
      String q2 = "insert into user(name,username,password,email) values('AHA','xxxAA','xxxA','xSDxx')"; 
      String query = "query=" + q2; 
      String type = "application/x-www-form-urlencoded"; 
      conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("User-Agent", agent); 
      conn.setRequestProperty("Content-Type", type); 
      conn.setRequestProperty("Content-Length", "" + query.length()); 

      OutputStream out = conn.getOutputStream(); 
      out.write(query.getBytes()); 
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      while ((inputLine = in.readLine()) != null) { 
       data = data + inputLine + "\n"; 
      } 
      //data = StringParser.StringPars(data); 
      in.close(); 
      int rc = conn.getResponseCode(); 
      System.out.print("Response Code = " + rc + "\n"); 
      String rm = conn.getResponseMessage(); 
      System.out.print("Response Message = " + rm + "\n"); 
      Toast.makeText(this, "Response Code = " + rc + "\n", Toast.LENGTH_SHORT).show(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Toast.makeText(this, e.getMessage() + "\n" + e.getCause(), Toast.LENGTH_SHORT).show(); 
     } finally { 
      conn.disconnect(); 
     } 
    } 
} 
+0

あなたのlogcat(e.printStackTrace()から)に何が表示されますか? – theblitz

答えて

0

1)を追加します<uses-permission android:name="android.permission.INTERNET" />

関連する問題