0

在 logcat 中启动 android 和 tomcat 时出现此错误。下面是我的代码。我正在做的是尝试将文本写入 tomcat 数据库,以便它能够在不输入数据库的情况下显示名称。

<?xml version="1.0" encoding="utf-8"?>
package="project.splash"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="project.splashscreen.MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="project.splashscreen.MainForm"
        android:label="@string/title_activity_main_form" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="project.splash.LoginActivity"
        android:label="@string/title_activity_login"
        android:windowSoftInputMode="adjustResize|stateVisible" >
    </activity>
    <activity
        android:name="project.splashscreen.LogActivity"
        android:label="@string/title_activity_log" >
    </activity>
    <activity
        android:name="project.splash.MainActivityl"
        android:label="@string/title_activity_main_activityl" >
    </activity>
</application>

我的 android 主要活动 java 文件

public class LogActivity extends Activity {
Button btn;
EditText one,two;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_log);

    one = (EditText)findViewById(R.id.playern1);
    two= (EditText)findViewById(R.id.playern2);     
 btn = (Button)findViewById(R.id.button1);

btn.setOnClickListener(new OnClickListener(){

    SharedPreferences myPrefs = getSharedPreferences("PREF_COUNT",0);
    SharedPreferences.Editor myEditor = myPrefs.edit();
    public void onClick(View v){

        Intent log = new Intent(LogActivity.this,MainForm.class);
        log.putExtra("pass1",two.getText().toString());
        log.putExtra("pass2",one.getText().toString());

                //  String names = myPrefs.getString("name","");            
                    myEditor.putString("pass1",two.getText().toString());
                    myEditor.putString("pass2",one.getText().toString());
                    myEditor.commit();
                // new NetworkTask().execute("http://10.0.2.2:8080/MDAD/read/team7tomcat.ReadServlet");
                 new NetworkTask2().execute("http://10.0.2.2:8080/MDAD/write/team7tomcat.WriteServlet");
                    startActivity(log);
    }
});

}



private class NetworkTask2 extends AsyncTask<String, Void, String>{
        String line="";
        protected String doInBackground(String... params) {
            String address=params[0];
            try{
            URL url = new URL(address);
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            con.setRequestMethod("POST");
            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());     
            out.write(one.getText().toString());//get converted value
            out.write(two.getText().toString());//get converted value
            out.flush();
            out.close();

            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            line=br.readLine();// read back response from server
            }
        catch(IOException e){Log.e("error", "mm", e); }

            return line;
        }
        protected void onPostExecute(String result) {
            one.setText(result);
            //two.setText(result);
       }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.log, menu);
        return true;

    }
}

汤姆猫写servlet

public class WriteServlet extends HttpServlet {
private static final String db_server = "localhost:3306/";
private static final String db_name = "test";
private Connection con = null;

public void init(ServletConfig config) throws ServletException {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (Exception e) {
    }

    try {
        con = DriverManager.getConnection("jdbc:mysql://" + db_server
                + db_name, "root", "1");
    } catch (Exception e) {
    }
} // End init()

public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    BufferedReader reader = new BufferedReader(new InputStreamReader(
            req.getInputStream()));
    String name;

    while ((name = reader.readLine()) != null) // read from mobile device
    {
        Statement stmt;
        //String sgd = name;
        //String name = sgd.toString();

        //String sgd = name;
          name.toString();
        try {
            stmt = con.createStatement();
            stmt.executeUpdate("INSERT INTO tm7 VALUES ('" + name
                    + "') ");

        //  ((PrintWriter) con).flush(); 
         //    con.close();
        } catch (Exception e) {
        //  e.printStackTrace(); // at least

        }

        System.out.println(name);
    }

    PrintWriter out = resp.getWriter();
    out.println(" server received");
}

}

4

0 回答 0