flask 转 java_Python/Flask服务器端Java/HttpURLConnection客户端REST实现

我目前正在实现一个REST接口到我的web应用程序,从一个桌面应用程序,以补充服务。我们将Python和Flask一起用于REST服务器实现。在

我们有一种情况,Java客户机将请求POST,python服务器端将处理POST,直到它需要连接到MySQL数据库。在

下面是python/flask代码:@app.route("/update_project/", methods=['GET','POST'])

def updateWPProject(project_data):

"""project_data = user+++++pw+++++pj_jsondoc.

This will update an existing project data or insert a non-existant project data item."""

usrpw = project_data.split("+++++")

dblogin = wplogin(usr=usrpw[0], pw=usrpw[1])

if dblogin[0] == 'SUCCESS':

db_name = dblogin[1][0].strip()

db_user = dblogin[1][1].strip()

db_pw = dblogin[1][2].strip()

if flask.request.method == 'POST':

fname = flask.request.form['fName']

if fname:

pj = Project()

try:

pj = json.loads(fname)

except TypeError:

sys.stdout.write("JSON conversion error...%s" % sys.exc_info()[0])

except AttributeError:

sys.stdout.write("JSON conversion error...%s" % sys.exc_info()[0])

updateProject(db_name,db_user,db_pw,pj)

#return redirect(url_for('/'))

else:

return 'Login failure.'

pass

在到达“updateProject(db_name,db_user,db_pw,pj)”行之前,这一切都很正常。然后它将转到该函数,并在光标处返回500=连接光标未完成函数。在

^{pr2}$

在Java客户端,我使用以下代码从客户端发送POST请求和数据。在public void DB_NewProjectREST(ProjectData proj, WPUser usr) {

// public static String excutePost(String targetURL, String urlParameters)

//URL url;

HttpURLConnection connection = null;

///First, all the GSON/JSon stuff up front

Gson gson = new Gson();

//convert java object to JSON format

String json = gson.toJson(proj);

//Then credentials and send string

String send_string = usr.getUserEmail()+"+++++"+usr.getUserHash();

try {

//Create connection

URL url = new URL("http://127.0.0.1:5000/update_project/"+send_string);

String urlParameters = "fName=" + URLEncoder.encode(json, "UTF-8");

connection = (HttpURLConnection)url.openConnection();

connection.setRequestMethod("POST");

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));

connection.setRequestProperty("Content-Language", "en-US");

connection.setUseCaches(false);

connection.setDoInput(true);

connection.setDoOutput(true);

//Send request

DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());

wr.writeBytes (urlParameters);

wr.flush ();

wr.close ();

//Get Response

InputStream is = connection.getInputStream();

BufferedReader rd = new BufferedReader(new InputStreamReader(is));

String line;

StringBuffer response = new StringBuffer();

while((line = rd.readLine()) != null) {

response.append(line);

response.append('\r');

}

rd.close();

} catch (Exception e) {

e.printStackTrace();

//return null;

}finally {

if(connection != null) {

connection.disconnect();

}

}

}

似乎我必须向客户机发送某种响应(对于输入流),而这正是我的python代码突然返回的地方数据从java客户机获取到python/flask代码,那么有没有任何方法可以快速返回“OK”并继续处理db更新。或者,python updateProject函数中的游标连接调用是否会干扰http连接?在

感谢任何帮助。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值