我正在开发一个使用java-servlet访问数据库的Andriod应用程序。 由于我正在使用sdk 23,以前的一些模块已被弃用,所以我使用URLconnection类来获取与servlet的连接。 但是通过运行下面的代码,我的应用程序停止工作。
App基于Navigation抽屉活动构建,下面的代码在一个片段类中实现。是的,我已经为网络
设置了权限package com.example.nirmal.gaminghub;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.lang.Object;
public class GameList extends Fragment {
TextView gm_lst=(TextView)getView().findViewById(R.id.game_list);
Button show;
String str ="" ;
public GameList() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
new AsyncTask<String, String, String>() {
protected String doInBackground(String... url) {
try {
URL openUrl = new URL("http://localhost:8080/GamingHub/ShowDataServlet");
HttpURLConnection connection = (HttpURLConnection) openUrl.openConnection();
connection.setDoInput(true);
// Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_LONG).show();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
StringBuilder getOutput = new StringBuilder();
while ((line = br.readLine()) != null) {
getOutput.append(line);
}
br.close();
str=line;
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
protected void OnPostExecute(String otpt)
{
gm_lst.setText(otpt);
}
}.execute();
gm_lst.setText(str);
return inflater.inflate(R.layout.fragment_game_list, container, false);
}
}
下面的代码是针对servlet的,它工作正常。
package com.gaming_hub;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowDataServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, SQLException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/gaming_hub","root","");
String sql="SELECT * from games";
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
// DataOutputStream dout=new DataOutputStream();
while(rs.next())
{
out.println(rs.getString(1));
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ShowDataServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(ShowDataServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ShowDataServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(ShowDataServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
答案 0 :(得分:1)
堆栈跟踪可能会清楚地指出问题出在代码行中:
TextView gm_lst=(TextView)getView().findViewById(R.id.game_list);
问题与Fragment
生命周期有关。你不能(成功地)在那时调用findViewById()
作为Fragment
并且它的视图还不存在。
例如,onViewCreated()
方法可以安全地调用findViewById()
。所以你可以尝试类似的东西:
public class GameList extends Fragment {
TextView gm_lst;
Button show;
String str ="" ;
public GameList() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_game_list, container, false);
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
gm_lst = (TextView)getView().findViewById(R.id.game_list);
new AsyncTask<String, String, String>() {
protected String doInBackground(String... url) {
try {
URL openUrl = new URL("http://localhost:8080/GamingHub/ShowDataServlet");
HttpURLConnection connection = (HttpURLConnection) openUrl.openConnection();
connection.setDoInput(true);
// Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_LONG).show();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
StringBuilder getOutput = new StringBuilder();
while ((line = br.readLine()) != null) {
getOutput.append(line);
}
br.close();
str=line;
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
protected void OnPostExecute(String otpt)
{
gm_lst.setText(otpt);
}
}.execute();
gm_lst.setText(str);
}
然后另一个问题是,您指定了str = line;
并且仅在您可能想要br.readLine()
的内容时获取最后getOutput
返回的内容。