API getSongURLbyid working

This commit is contained in:
Yorick Rommers
2015-10-29 20:37:36 +01:00
parent 2de025edf7
commit 415e87b787
5 changed files with 32 additions and 14 deletions
+14 -1
View File
@@ -4,13 +4,26 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using MusicPlayer; using MusicPlayer;
using Newtonsoft.Json.Linq;
namespace MusicPlayer namespace MusicPlayer
{ {
internal class APIHandler internal class APIHandler
{ {
public APIHandler() private NetworkHandler nw;
public APIHandler(NetworkHandler nw)
{ {
this.nw = nw;
}
public string GetSongURLByID(string id)
{
JObject o = nw.SendString("getsongbyid?id=" + id);
if (o["result"].ToString() == "OK") {
return o["songurl"].ToString();
}
return o["errormsg"].ToString();
} }
-1
View File
@@ -18,7 +18,6 @@ namespace MusicPlayer
public MainForm() public MainForm()
{ {
InitializeComponent(); InitializeComponent();
// nw.SendString("GET / HTTP/1.1");
} }
+7 -2
View File
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
@@ -33,6 +33,10 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
@@ -46,7 +50,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="MainForm.cs" > <Compile Include="MainForm.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>
<Compile Include="Album.cs" /> <Compile Include="Album.cs" />
@@ -74,6 +78,7 @@
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
</Compile> </Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings"> <None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <LastGenOutput>Settings.Designer.cs</LastGenOutput>
+6 -5
View File
@@ -4,6 +4,8 @@ using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Net; using System.Net;
using System.IO; using System.IO;
using Newtonsoft.Json.Linq;
namespace MusicPlayer namespace MusicPlayer
{ {
@@ -11,15 +13,13 @@ namespace MusicPlayer
{ {
private int port = 8585; private int port = 8585;
private string ip; private string ip;
private APIHandler api;
public NetworkHandler(string ip, APIHandler apihandler) public NetworkHandler(string ip)
{ {
this.ip = ip; this.ip = ip;
this.api = apihandler;
} }
public void SendString(string m) public JObject SendString(string m)
{ {
HttpWebRequest server = (HttpWebRequest)WebRequest.Create(ip+":"+port+"/"+m); HttpWebRequest server = (HttpWebRequest)WebRequest.Create(ip+":"+port+"/"+m);
server.KeepAlive = false; server.KeepAlive = false;
@@ -35,10 +35,11 @@ namespace MusicPlayer
data +=outputData; data +=outputData;
count = streamRead.Read(readBuff, 0, 256); count = streamRead.Read(readBuff, 0, 256);
} }
System.Console.WriteLine(data); JObject o = JObject.Parse(data);
respond.Close(); respond.Close();
streamResponse.Close(); streamResponse.Close();
streamRead.Close(); streamRead.Close();
return o;
} }
} }
} }
+3 -3
View File
@@ -14,9 +14,9 @@ namespace MusicPlayer
[STAThread] [STAThread]
static void Main() static void Main()
{ {
APIHandler api = new APIHandler(); NetworkHandler nw = new NetworkHandler("http://www.imegumii.nl");
NetworkHandler nw = new NetworkHandler("http://www.imegumii.nl", api); APIHandler api = new APIHandler(nw);
nw.SendString("getsongbyid?id=102"); Console.WriteLine(api.GetSongURLByID("102"));
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm()); Application.Run(new MainForm());